|
-
March 23rd, 2009, 02:49 AM
#1
Quirk String.Remove vs StringBuilder.Remove
I found a quirk that may be helpful for someone else to know, if they run into the same mistake.
EDIT: By quirk, I mean that a newbie may not be able to predict the behavior, thus causing them to make a mistake.
You can remove part of a string "In place" by using the StringBuilder.Remove method.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sb As New System.Text.StringBuilder("randomstring2342")
sb.Remove(0, 6)
MessageBox.Show(sb.ToString)
End Sub
However, you can not remove it "In place", if you are using String.Remove.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String = "randomstring2342"
s.Remove(0, 6)
MessageBox.Show(s)
End Sub
You have to use the return value instead.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String = "randomstring2342"
s = s.Remove(0, 6)
MessageBox.Show(s)
End Sub
Although, the stringbuilder allows you to use the return value too.
One might not expect that you'd be able to set it "In place" or by it's "Return value".
You may expect that a String can be set in place though.
Last edited by TT(n); March 26th, 2009 at 11:00 AM.
-
March 23rd, 2009, 07:51 AM
#2
Re: Quirk String.Remove vs StringBuilder.Remove
it is not a quirk. from the documentation of String:
"Once you assign a string to a String variable, that string is immutable, which means you cannot change its length or contents. When you alter a string in any way, Visual Basic creates a new string and abandons the previous one. The String variable then points to the new string."
and stringbuilder
"This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters."
-
March 23rd, 2009, 08:27 AM
#3
Re: Quirk String.Remove vs StringBuilder.Remove
It so is a quirk.
I didn't say a bug, which would disagree with the documentation.
I was being helpful, not asking a question.
-
March 23rd, 2009, 01:43 PM
#4
Re: Quirk String.Remove vs StringBuilder.Remove
No it is not a quirk. Strings are immutable by design, that is why you can't do that with a string.
Immutable means that String becomes Thread Safe and has a better performance.
-
March 23rd, 2009, 02:42 PM
#5
Re: Quirk String.Remove vs StringBuilder.Remove
quirk
1 a: an abrupt twist or curve b: a peculiar trait
a = no
b = no
-
March 23rd, 2009, 03:06 PM
#6
Re: Quirk String.Remove vs StringBuilder.Remove
I've known for years that strings are immutable.
Stringbuilder has better performance if you are going to manipulate lots of strings. I'm not sure what performance gain Shuja is speaking of.
It recently came up while I was teaching someone how to work with strings. Its still not obvious or expected to someone who doesn't already know.
Hence Vagary (C=True), a unpredictable twist that actually makes sense in the end.
Yes it's a quirk.
I win.
-
March 23rd, 2009, 03:38 PM
#7
Re: Quirk String.Remove vs StringBuilder.Remove
the sun came up
quirk - the sun came up and i could not see it ??????????? it was a cloud covered morning.
a lot of people would see this post and decide that strings do something quirky, and that is not the case.
if your post had been Strings and StringBuilder Different, well DUH! but if it is important for you to win this semantic debate, claim victory.
Code:
Dim s As String = "randomstring2342"
Dim sb As New System.Text.StringBuilder()
sb.Append(s)
'at this point s and sb have the same values stored "randomstring2342"
s.Remove(0, 6) 'this does nothing to s
s = s.Remove(0, 6) 's now equals "string2342"
sb.Remove(0, 6) 'sb now equals "string2342"
-
March 23rd, 2009, 05:45 PM
#8
Re: Quirk String.Remove vs StringBuilder.Remove
if it is important for you to win this semantic debate, claim victory.
Wow, I guess you're the better person, for allowing me to claim victory.
Clearly you just want to help other members out, and this is not some immature attempt to make yourself look better.
-
March 23rd, 2009, 06:00 PM
#9
Re: Quirk String.Remove vs StringBuilder.Remove
You brought up winning, I didn't. I don't care. There is nothing "quirky" about your examples. String and StringBuilder are different.
-
March 24th, 2009, 12:28 AM
#10
Re: Quirk String.Remove vs StringBuilder.Remove
-
March 24th, 2009, 03:54 AM
#11
Re: Quirk String.Remove vs StringBuilder.Remove
Basically you are missing the point of why immutability is required at first place.
Read the background section here
http://en.wikipedia.org/wiki/Immutable_object
-
March 24th, 2009, 07:59 AM
#12
Re: Quirk String.Remove vs StringBuilder.Remove
Shuja,
Although I think you are genuiely trying to be helpful, as you can see I don't need any schooling with simple wikipedia links.
As I've illustrated already, I was teaching someone that ran into an issue with the Remove method, that caused his program to crash.
I learned how to use strings, and StringBuilder way back in 2004-2005. At that time, I also ran into the issue when coverting an example that used StringBuilders, into one that uses regular strings. Suddenly the Remove function fails(as it should), and I had to find out why. A quirky obstacle at the time.
Sure it was a newbie mistake, or undersight.
That's why I posted to help others, that don't know yet.
It doesn't necessarily mean that they are dumb either, as Oblio implies.
OFF TOPIC(some more)
----------------------
Speed Strings vs. StringBuilder.
There are alot indications that StringBuilder is generally faster, and only a few that show Stings having a slight speed advantage in isolated cases.
Lot's of variables can taint any single deterministic test, so I can only rely on what I've seen over the years:
StringBuilder = good
With Strings a new memory address is allocated each time, so this seems to be one possible reason that Strings lose the speed advantage they should enjoy, when there are many manipulations.
Here is my isolated test on one machine (xp sp3 AMD ASUS).
I used both the append method, and the remove method, and found StringBuilder definately faster in all cases.
.REMOVE (3.8... x faster)
String
00:00:00.0011440
StringBuilder
00:00:00.0002992
This test used a string with a length of only a 1000.
This speed difference only increases as the string gets larger.
Code:
Dim StringTimeSpan As Int32
Dim BuilderTimeSpan As Int32
Private tString As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tString = "ha98h34fp98hap9w38y9prhshefhkshahsdifhk34hlkh2lkhlk34h2lkjh3rlk2hlhfh3h839hf98hs9dfg90s8ydf908sydf8s9hdf9h9hgf98sh98hdfg9s8hd9ghs9dhg9s8hf9gywy9yyy8yf8yw8etwtr3jhrg2j3grjg23uhrh0b29u0u0bu0bu209n023n 04707v2 08975 90275 0v729073095702n03475n0720934750n237045027vn50720572075b072n507n20785n0287n0578n20785nv2f095378j92387502385nh209875hn02978hj50782hn0572h57777777777777777777777777777777777777777777777777777777777777777777777777gggggggggggggggggggggggggggggggggggggggggggggggggggggasdgfawet34tgn8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888"
' tString &= tString 'double the length...
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If UseString.CheckState = CheckState.Checked Then UseStringTest()
If UseBuilder.CheckState = CheckState.Checked Then UseBuilderTest()
End Sub
Private Sub UseStringTest()
Dim myStopwatch As Stopwatch = New Stopwatch
Dim LoopLimit As Int32 = tString.Length
Dim TestString As String = tString
Dim myTimeSpan As New TimeSpan
myStopwatch.Reset()
myStopwatch.Start()
For i As Int32 = 1 To LoopLimit - 1
TestString = TestString.Remove(0, 1)
Next
myStopwatch.Stop()
myTimeSpan = myStopwatch.Elapsed
StringTimeSpan = myTimeSpan.Milliseconds
UsingString.Text = myTimeSpan.ToString
End Sub
Private Sub UseBuilderTest()
Dim myStopwatch As Stopwatch = New Stopwatch
Dim LoopLimit As Int32 = tString.Length
Dim TestAppend As New System.Text.StringBuilder("")
Dim myTimeSpan As New TimeSpan
TestAppend.Append(tString)
myStopwatch.Reset()
myStopwatch.Start()
For i As Int32 = 1 To LoopLimit - 1
TestAppend.Remove(0, 1)
Next
myStopwatch.Stop()
myTimeSpan = myStopwatch.Elapsed
BuilderTimeSpan = myTimeSpan.Milliseconds
UsingBuilder.Text = myTimeSpan.ToString
End Sub
If anyone comes up with different results, or has an issue with the testing code above, please post and correct me.
Shuja,
Did you mean safety or speed performance?
Or both?
Last edited by TT(n); March 24th, 2009 at 09:55 AM.
-
March 24th, 2009, 01:25 PM
#13
Re: Quirk String.Remove vs StringBuilder.Remove
STOP putting words in my mouth. "It doesn't necessarily mean that they are dumb either, as Oblio implies." I didn't imply anything. I said
1 - it isn't quirky
2 - they are different
-
March 24th, 2009, 01:57 PM
#14
Re: Quirk String.Remove vs StringBuilder.Remove
 Originally Posted by TT(n)
Although I think you are genuiely trying to be helpful, as you can see I don't need any schooling with simple wikipedia links.
I was not trying to do any schooling though. Usually if a thread is posted here, it implies that it is a question.
When it is a FAQ or just an information, it should go into other section.
With regards to your test, keep in mind, StringBuilder has been made available for different kind of operations and string has a different use. Remove method would create a new instance of string in memory, however StringBuilder works in a different way.
-
March 24th, 2009, 03:04 PM
#15
Re: Quirk String.Remove vs StringBuilder.Remove
Usually if a thread is posted here, it implies that it is a question.
Well did you actually read the posts. It's clearly not a question.
Then I re-enforced this fact again during the thread.
EDIT: And no it shouldn't be moved to another section. Do not.
With regards to your test, keep in mind, StringBuilder has been made available for different kind of operations and string has a different use. Remove method would create a new instance of string in memory, however StringBuilder works in a different way.
Wow really.
You didn't answer my real question about what kind of performance gain you were speaking of.
Safety, speed, or both?
Your sentence can be interpreted in several ways.
Immutable means that String becomes Thread Safe and has a better performance.
Does And mean Also?
It's only safer in some cases for newbies, that may not be keeping track of things, or changing the value between threads.
It's not really an issue for me, when I use it.
Obviously I've shown that StringBuilder is faster in most cases, and so has better performance.
Basically you are missing the point of why immutability is required at first place.
Ya okay, where did that come from?
I'm not the one missing any points here.
Last edited by TT(n); March 24th, 2009 at 03:12 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|