How should I concatenate strings?

2019-01-14 16:09发布

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:

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?

9条回答
戒情不戒烟
2楼-- · 2019-01-14 16:45
var str3 = new StringBuilder
    .AppendFormat("abc{0}{1}", dynamicString, dynamicString2).ToString(); 

the code above is the fastest. so use if you want it fast. use anything else if you dont care.

查看更多
3楼-- · 2019-01-14 16:48

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:

string result=String.Format(
    "Today {0} scored {1} {2} and {3} points against {4}",..);

//looks nicer than
string result = "Today " + playerName + " scored " + goalCount + " " + 
    scoreType + " and " + pointCount + " against " + opposingTeam;

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.

查看更多
Luminary・发光体
4楼-- · 2019-01-14 16:48

It is important to remember that strings do not behave like regular objets. Take the following code:

string s3 = "Hello ";
string s3 += "World";

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.

查看更多
欢心
5楼-- · 2019-01-14 16:49

My rule of thumb is to use String.Format if you are doing a relatively small amount of concatination (<100) and StringBuilder for times where the concatination is going to be large or is potentially going to be large. I use String.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

查看更多
仙女界的扛把子
6楼-- · 2019-01-14 16:52

Gathering information from all the answers it turns out to behave like this:

The + operator is the same as the String.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 the String.Concat generates the expression str = str1 + str2; even if they are static.

String.Format is the same as StringBuilder.. (example 3) except that the String.Format does a validation of params and instantiate the internal StringBuilder 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.

查看更多
地球回转人心会变
7楼-- · 2019-01-14 16:57

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.

查看更多
登录 后发表回答