String vs StringBuilder

  • Thread starter Thread starter Chizl
  • Start date Start date
C

Chizl

Can someone give me a little more detail on why use StringBuilder vs String?
 
Chizl said:
Can someone give me a little more detail on why use StringBuilder vs
String?

Strings in .Net are immutable. This means that if you change something in
a string, a new one will be allocated, rather than making the changes on the
existing one. For instance, if you have a loop like this:

string s = "";
for (int i=0; i<1000; i++)
{
s+=i.ToString();
}

This will cause one thousand allocations of strings, and discarding of
the previously allocated ones. This will be much slower than we expect it to
be.

The StringBuilder class remedies this. It allocates a buffer and makes
the changes on it, only allocating a new buffer if the previous one happens
to become too small. This will improve the performance of the loop:

StringBuilder sb = new StringBuilder();
for (int i=0; i<1000; i++)
{
sb.Append(i.ToString());
}
string s = sb.ToString();
 
When you use a string and change it, the framework creates a new string in
memory abd dereferences the old one. With a stringbuilder the object isn't
destroyed each time, instead its altered, so if you are doing lots of
concationation, looping etc. on a string then stringbuilder can be more
efficient.

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
 
Since C# strings are immutable, an existing string cannot be modified. So, if
one tries to change a string either with the concatenation operator (+) or
with the Insert, PadLeft, PadRight, Replace, or SubString methods, an
entirely new string is created—leaving the original string intact.

Therefore, operations which would alter strings—instead—cause additional
memory to be allocated. Memory is a scarce resource. And, memory allocations
are expensive in terms of memory and performance. Consequently, sometimes
String class usage should be avoided.

The StringBuilder class is designed for situations when one needs to work
with a single string and make an arbitrary number of iterative changes to it.
Many StringBuilder class methods are the same as those of the String class.
However, the string content of a StringBuilder class can be changed without
the necessity of allocating additional memory. Thus, operations on the
StringBuilder class will be much faster than operations on the String class
in certain situations. Paradoxically, just the the opposite can be true in
other situations.

The String class is optimized and quite efficient for most cases. On the
other hand, if strings must be modified, then the String class can be a real
resource waster. It must be appreciated that the String class is really very
intelligent in its memory handling in most everyday programming situations.

Also see
http://blogs.msdn.com/larryosterman/archive/2004/04/06/108686.aspx
and
http://channel9.msdn.com/ShowPost.aspx?PostID=14932
 
Misbah Arefin said:
Since C# strings are immutable, an existing string cannot be modified. So,
if
one tries to change a string either with the concatenation operator (+) or
with the Insert, PadLeft, PadRight, Replace, or SubString methods, an
entirely new string is created-leaving the original string intact.

Therefore, operations which would alter strings-instead-cause additional
memory to be allocated. Memory is a scarce resource. And, memory
allocations
are expensive in terms of memory and performance. Consequently, sometimes
String class usage should be avoided.

With the .NET garbage collector, memory allocation is an extremely cheap
operation, practically speaking no cost at all.

The copy of the entire buffer to the new location is far more expensive, as
well as having the garbage collector run more often (although objects used
briefly and dereferenced before the next GC don't add to the collection
time, the fact that the GC has to analyze the reachable objects more often
is a hit).

Everything else that was written is correct. StringBuilders should be used
when modifying a string repeatedly.
 
This will cause one thousand allocations of strings

I once wrote a substitute routine for the StringReplace function in Delphi.
The main time saver was in writing the result to a buffer that grew as
required, just as StringBuilder does. I was able to reduce a StringReplace
that took over 4 hours down to less than 1 second. So it really does make a
lot more difference than you would expect, especially when dealing with
large strings and many reassignments!


Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top