A StringBuilder's main functionality is , as it's name implies, to build
strings. The fact is, in .Net, if you need to build a long string (using a
long loop for example) it would be much slower to accomplish by using
simple concatenation than it would be using a StringBuilder.
The speed improvement is very big. This is because internally, the
StringBuilder uses string pointers for concatenation, while a regular
concat operation actually creates a new string from each two concatenated
string, so if you do "a" + "b" you actually get 3 seperate string back,
a,c, and the result. in a loop doing this this is very slow. A
StringBuilder does not create new string instances and so is much faster.
When to use: Whenever you have a non trivial string that you need to build
dynamically using a loop (when creating XML strings for example).
Roy Osherove
http://www.iserializable.com
On Sun, 9 Nov 2003 19:06:31 +0100, <gi***@web.de> wrote:
Hi!
I'm very interesting in when to use exactly the StringBuilder?
For example for something like this?:
String strTest1 = "This";
String strTest2 = "Test";
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append("is a "). Append(stbTest);
can someone provide some sample codes when to use a StringBuilder?
And can someone tell me his experience about the performance of a
StringBuilder?
Regards,
gicio