From String to StringBuilder

ayozzhero

Well-known member
Joined
Apr 6, 2005
Messages
186
Location
Malaysia
Programming Experience
1-3
I am new to StringBuilder. Commonly, I would write my code like this:
VB.NET:
[color=Blue]Dim[/color] myString [color=Blue]As String[/color]
[color=Blue]For [/color]i [color=Blue]As Integer[/color] = 0 [color=Blue]To[/color] 10
	myString = i
	[color=DarkGreen]'some other codes go here[/color]
[color=Blue]Next[/color]

But if I want to change MyString to StringBuilder, how should the code myString = i be?

Thanks for helping.
 
Last edited:
Append will append the new value to the existing string (in the previous case, Messagebox.Show("str") will be 12345678910). What I need is a total replacement of the string itself (recycle the variable), as in:
VB.NET:
Dim myStr as string
mystr = "abc"
'some codes here
mystr = "def"

Thanks for your fast response.
 
From what I've read, StringBuilder is more efficient and String keeps creating a new copy everytime the value is changed. I am not sure whether that can be done using StringBuilder or not.
 
If i raise my eyes, i would dig more deeper to answer ayozzohero!
actually the String class is designed in such a way that it can use even stack while storing the characters and hence we need a new copy while shanging the contents. as for as String Builder is concern if u use Append and Insert methods it just get more allocation in heap and do the trick. which is certainly not possible in case of String stored in stack. Therefore it is suggested that if u need to insert replace or append a character or characters, use string builder.
But if u need to replace the whole string then u are already creating a new copy, therefore getting space in stack through static binind is much more efficient thing.
cheers :)
 
Back
Top