Possible Duplicate:
Stringbuilder vs String.Concat
As I understand it, String are immutable, so if you do "one " + "two"
, rather than reusing the memory of these two strings, a new string is created with value "one two". This becomes a problem if you do something like:
String output = "";
for (int i = 0; i < 20; i++){
output = output + ii.toString();
}
Because 19 (or 39?) strings are created and discarded to produce the final desired output.
String builders are mutable, so you can add strings together efficiently, and then convert them into a string when you are done.
I assume there is some overhead in creating string builders, and also the clever compilers used today optimize away lots of string addition problems. So my question is: how many strings do I need to add together before it becomes more efficient to use a StringBuilder instead? Or are my assumptions incorrect?