views:

266

answers:

11

Lot of questions has been already asked about the differences between string and string builder and most of the people suggest that string builder is faster than string. I am curious to know if string builder is too good so why string is there? Moreover, can some body give me an example where string will be more usefull than string builder?

+1  A: 

Would you really use a string builder here?

Console.WriteLine("MyString");

StringBuilder is great when you need to do large amounts of concatenation to build a string.

Jon Skeet has a great aricle on this here: http://www.yoda.arachsys.com/csharp/stringbuilder.html

Check out the section that starts "So I Should Use StringBuilder Everywhere, Right?"

He writes:

The important difference between this example and the previous one is that we can easily present all the strings which need to be concatenated together in one call to String.Concat. That means that no intermediate strings are needed. StringBuilder is efficient in the first example because it acts as a container for the intermediate result without having to copy that result each time - when there's no intermediate result anyway, it has no advantage.


brendan
thanks brendan..the link is really good
DJay
+2  A: 

StringBuilder is better when you need to change the value. Otherwise, there's no need for it.

If I need to use the same string value in 10 places, as it is, I'll use String. If I need to change it 10 times, StringBuilder.

Rox
+7  A: 

StringBuilder is a better option when constructing and modifying strings through heavy looping or iterations.

Simple string operations such as a few concatenations or simply storing a string value suits a standard string object far better.

Similar question? http://stackoverflow.com/questions/73883/string-vs-stringbuilder

KP
A: 

StringBuilder is not faster than using a string as far as just creating a string.

StringBuilder can be faster when you have to concatenate several strings together (like in loop). You are going to pay a performance penalty when you have to convert the string builder contents to a string.

One of the real advantages of StringBuilder over string is that you can pass it through several methods and append strings along the way. You can't do that by passing a string object. So if you have a recursive operation where you are building like a JSON object or something, you can pass the StringBuilder through and append the values.

Kevin
+10  A: 

StringBuilder is, as its name suggested, is used to build strings, whereas strings are immutable character-values. Use strings when you want to pass around character data, use StringBuilder when you want to manipulate character data.

inflagranti
A: 

A string is good because it is immutable. And that immutable is good, is something that functional languages are prove of.

Because if you have a (reference to) string "This is a string" you can be sure that it will have the same value forever. While the value of a StringBuilder might be changed (by a method call, another thread etc).

But, sometimes you need raw, pure speed and memory usage optimizations. In that case you use a stringbuilder.

An example:

 var a = "This is a string";
 var b = a;
 a += ". And some part is added";   // a now references another string in memory

 var aSB = new StringBuilder("This is a string");
 var bSB = aSB
 aSB.Append(". And some part is added"); // aSB and bSB still reference the same memory spot

 Console.WriteLine(b);   // ==> This is a string
 Console.WriteLine(bSB); // ==> This is a string. And some part is added
GvS
A: 

If you can read french, here is a good article that may help you

hoang
Mmmmm, I can read it, but I don't understand.
GvS
A: 

In brief, it's a trade-off between mutable and immutable

See this:StringBuilder vs String

rhapsodyn
+1  A: 

It's not a case of which is more useful...

A String is a String - one or more characters next to eachother. If you want to change a string in someway, it will simply create more strings because they are immutable.

A StringBuilder is a class which creates strings. It provides a means of constructing them without creating lots of reduntant strings in memory. The end result will always be a String.

Don't do this

string s = "my string";
s += " is now a little longer";

or

s = s + " is now longer again";

That would create 5 strings in memory (in reality, see below).

Do this:

StringBuilder sb = new StringBuilder();
sb.Append("my string");
sb.Append(" is now a little longer");
sb.Append(" is now longer again");
string s = sb.ToString();

That would create 1 string in memory (again, see below).

You can do this:

string s = "It is now " + DateTime.Now + ".";

This only creates 1 string in memory.

As a side-note, creating a StringBuilder does take a certain amount of memory anyway. As a rough rule of thumb:

  • Always use a StringBuilder if you're concatenating strings in a loop.
  • Use a StringBuilder if you're concatenating a string more than 4 times.
David Neale
string.Concat is also a viable solution to your example. `string s = string.Concat("my string", "is now a little longer", "is now longer again");`
KP
Good point. I guess that's a matter of taste.
David Neale
A: 

An analysis of which to use is provided at: Concatenating Strings Efficiently.

Essentially, (although read the link for full analysis), use StringBuilder when concatenating in a non-trivial loop.

David_001
A: 

Consider 'The Sad Tragedy of Micro-Optimization Theater'.

Jim G.