Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use StringBuilder wisely?

I am little confused about using StringBuilder class, first:

A string object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is appended to the end of the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, then the new data is appended to the new buffer.

But where is the point of creating StringBuilder instance to avoid creating new one of String? It sounds like trading "one for one".

static void Main(string[] args) {     String foo = "123";     using (StringBuilder sb = new StringBuilder(foo)) // also sb isn't disposable, so there will be error     {         sb.Append("456");         foo = sb.ToString();     }      Console.WriteLine(foo);     Console.ReadKey(); } 

Why I shouldn't just use

+= 

Edit: Ok, I know now how to reuse one instance of StringBuilder (still don't know if this is right with code standards), but this isn't worth to use with just one string, isn't it?

like image 659
Bartłomiej Sobieszek Avatar asked Feb 08 '14 10:02

Bartłomiej Sobieszek


People also ask

Why do we use StringBuilder in Java?

StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

Why is StringBuilder mutable in Java?

StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates an immutable sequence of characters, the StringBuilder class provides an alternative to String Class, as it creates a mutable sequence of characters.

What is a string builder?

StringBuilder in Java is a class used to create a mutable, or in other words, a modifiable succession of characters. Like StringBuffer, the StringBuilder class is an alternative to the Java Strings Class, as the Strings class provides an immutable succession of characters.


1 Answers

Modifying immutable structures like strings must be done by copying the structure, and by that, consuming more memory and slowing the application's run time (also increasing GC time, etc...).

StringBuilder comes to solve this problem by using the same mutable object for manipulations.

However:

when concatenating a string in compile time as the following:

string myString = "123"; myString += "234"; myString += "345"; 

it will actually compile to something like that:

string myString = string.Concat("123", "234", "345"); 

this function is faster than working with StringBuilder for the number of strings entering the function is known.

so for compile-time-known string concatenations you should prefer string.Concat().

as for unknown number of string like in the following case:

string myString = "123"; if (Console.ReadLine() == "a") {     myString += "234"; } myString += "345"; 

Now the compiler can't use the string.Concat() function, however, StringBuilder appears to be more efficient in time and memory consumption only when the concatenation is done with 6-7 or more strings.

Bad practice usage:

StringBuilder myString = new StringBuilder("123"); myString.Append("234"); myString.Append("345"); 

Fine practice usage (note that if is used):

StringBuilder myString = new StringBuilder("123"); if (Console.ReadLine() == "a") {     myString.Append("234"); } myString.Append("345"); 

Best practice usage (note that while loop is used):

StringBuilder myString = new StringBuilder("123"); while (Console.ReadLine() == "a") {     myString.Append("234"); //Average loop times 4~ or more } myString.Append("345"); 
like image 163
Tamir Vered Avatar answered Oct 05 '22 19:10

Tamir Vered