stringbuilder vs string

  • Thread starter Thread starter John
  • Start date Start date
StringBuilder is much faster when you do string operations. String takes
less space in memory.

String type is immutable. There is no way to modify an olready created
string object. When you, for example, appending a character or another
string to a string, a new combined string is actually created every time.
StringBuilder is mutable and works much faster when you need to modify it's
content.

Vladimir [VB.Net team]


This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
A string object is immutable (once it has been created, it cannot be
changed). Contrary to popular belief,

Dim szTest as String

szTest = "Test1"

' This causes szTest to be assigned to a new object rather than "123"
getting bolted onto the back of it
szTest = szTest & "123"


Because a new object is created with every assignment, there is a slight
overhead when doing this repeatedly.

The string builder class provides a more efficient way to repeatedly add
bits on to a string. You provide it with an initial capacity (it grows
automatically, but will degrade performance if it grows too often). You can
add more bits of strings to it without the overhead of creating a new
object.

When you are finished building the string, use the ToSting method of the
StringBuilder to turn it into an ordinary string.

The performance difference will be too small to measure if you're only doing
a few concatenations, but if you're in a loop, building a big string (e.g.
HTML code for a web page) then StringBuilder has a definite advantage.

Hope this helps,

Trev.
 
Codemonkey,
The performance difference will be too small to measure if you're only doing
a few concatenations, but if you're in a loop, building a big string (e.g.
HTML code for a web page) then StringBuilder has a definite advantage.

I did believe that too, I did test it this week with a very small program,
and the performance difference is really unbelievable.
(Not of course for one occurrence in a program, but when it is repeated like
in a normal program, I now will fast go to stringbuilder).
Cor
 
John said:
What are the advantages of a stringbuilder compared to a string?

Strings are immutable in .NET, that means that every time you assign a
new value to the string, a new string will be created and the old object
will be deleted. If you compose a long string, for example in a loop,
by concatenating it with the '&' operator, this may be very costly.

In this situation you should better use a 'StringBuilder'. The
StringBuilder reserves some space and allows modifications of its text.
You can call its 'ToString' method to get the string represented by the
StringBuilder after finishing composing the text.
 
Back
Top