Stringbuilder
-
When would I use stringbuilder over just string? I just read about it in a book but it doesn't explain why you would use it.
-
When would I use stringbuilder over just string? I just read about it in a book but it doesn't explain why you would use it.
In .NET,
string
class is immutable. Simply put, it means that you cannot modify a string after it's creation. So, for examplestring someString = "Hello";
//Now if we write
someString += " World";The program will not add
World
to the existing string. Instead, it will create a new string object"Hello World"
and assigns this tosomeString
and discards the earlier object"Hello"
. So, this may not be a big issue if we are dealing with only a few strings. But, if we want to deal with several string operations, thenStringBuilder
is a good option. As explained here http://msdn.microsoft.com/en-us/library/y9sxk6fy.aspx[^] This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters. And after making all the modificationsToString
method of StringBuilder can be used to get the final string. I think this Code Project article StringBuilder vs. String, Fast String Operations with .NET 2.0[^] may be helpful. -
When would I use stringbuilder over just string? I just read about it in a book but it doesn't explain why you would use it.
-
This discussion[^] could be useful to you.
-
This discussion[^] could be useful to you.
-
In .NET,
string
class is immutable. Simply put, it means that you cannot modify a string after it's creation. So, for examplestring someString = "Hello";
//Now if we write
someString += " World";The program will not add
World
to the existing string. Instead, it will create a new string object"Hello World"
and assigns this tosomeString
and discards the earlier object"Hello"
. So, this may not be a big issue if we are dealing with only a few strings. But, if we want to deal with several string operations, thenStringBuilder
is a good option. As explained here http://msdn.microsoft.com/en-us/library/y9sxk6fy.aspx[^] This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters. And after making all the modificationsToString
method of StringBuilder can be used to get the final string. I think this Code Project article StringBuilder vs. String, Fast String Operations with .NET 2.0[^] may be helpful. -
When would I use stringbuilder over just string? I just read about it in a book but it doesn't explain why you would use it.
John Skeet has written about this[^] This is an excellent introduction to this topic. I once scored brownie points in a Job Interview for referring to this stuff when asked why I didn't use
StringBuilder
in a simple one-time concatenation during a programming test. The interviewer didn't beleive me at first, but a quick test showed similar results to the article.Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
John Skeet has written about this[^] This is an excellent introduction to this topic. I once scored brownie points in a Job Interview for referring to this stuff when asked why I didn't use
StringBuilder
in a simple one-time concatenation during a programming test. The interviewer didn't beleive me at first, but a quick test showed similar results to the article.Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^]