Are there differences between these examples? Which should I use in which case?
var str1 = "abc" + dynamicString + dynamicString2;
var str2 = String.Format("abc{0}{1}", dynamicString, dynamicString2);
var str3 = new StringBuilder("abc").
Append(dynamicString).
Append(dynamicString2).
ToString();
var str4 = String.Concat("abc", dynamicString, dynamicString2);
There are similar questions:
- Difference in String concatenation which only asks about the
+
operator, and it's not even mentioned in the answer that it is converted to String.Concat - What's the best string concatenation method which is not really related to my question, where it is asking for the best, and not a comparation of the possible ways to concatenate a string and their outputs, as this question does.
This question is asking about what happens in each case, what will be the real output of those examples? What are the differences about them? Where should I use them in which case?
the code above is the fastest. so use if you want it fast. use anything else if you dont care.
Use the + operator in your scenario.
I would only use the String.Format() method when you have a mix of variable and static data to hold in your string. For example:
I don't see the point of using a StringBuilder, since you're already dealing with three string literals.
I personally only use Concat when dealing with a String array.
It is important to remember that strings do not behave like regular objets. Take the following code:
This piece of code will create a new string on the heap and place "Hello" into it. Your string object on the stack will then point to it (just like a regular object).
Line 2 will then creatre a second string on the heap "Hello World" and point the object on the stack to it. The initial stack allocation still stands until the garbage collector is called.
So....if you have a load of these calls before the garbage collector is called you could be wasting a lot of memory.
My rule of thumb is to use
String.Format
if you are doing a relatively small amount of concatination (<100) andStringBuilder
for times where the concatination is going to be large or is potentially going to be large. I useString.Join
if I have an array and there isn't any formatting needed.You can also use the Aggregate function in LINQ if you have an enumerable collection: http://msdn.microsoft.com/en-us/library/bb548651.aspx
Gathering information from all the answers it turns out to behave like this:
The
+
operator is the same as theString.Concat
, this could be used on small concatenations outside a loop, can be used on small tasks.In compilation time, the
+
operator generate a single string if they are static, while theString.Concat
generates the expressionstr = str1 + str2;
even if they are static.String.Format
is the same asStringBuilder..
(example 3) except that theString.Format
does a validation of params and instantiate the internalStringBuilder
with the length of the parameters.String.Format
should be used when format string is needed, and to concat simple strings.StringBuilder
should be used when you need to concatenate big strings or in a loop.As long as you are not deailing with very many (100+) strings or with very large (Length > 10000) strings, the only criterion is readability.
For problems of this size, use the
+
. That+
overload was added to the string class for readability.Use
string.Format()
for more complicated compositions and when substitutions or formatting are required.Use a
StringBuilder
when combining many pieces (hundreds or more) or very large pieces (length >> 1000). StringBuilder has no readability features, it's just there for performance.