How should I concatenate strings?

前端 未结 9 1419
不知归路
不知归路 2020-12-06 00:43

Are there differences between these examples? Which should I use in which case?

var str1 = \"abc\" + dynamicString + dynamicString2;

var str2 = String.Form         


        
相关标签:
9条回答
  • 2020-12-06 01:05

    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

    0 讨论(0)
  • 2020-12-06 01:07

    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.

    0 讨论(0)
  • 2020-12-06 01:19

    @Xander. I believe you man. However my code shows sb is faster than string.format.

    Beat this:

    Stopwatch sw = new Stopwatch();
    sw.Start();
    
    for (int i = 0; i < 10000; i++)
    {
        string r = string.Format("ABC{0}{1}{2}", i, i-10, 
            "dasdkadlkdjakdljadlkjdlkadjalkdj");
    }
    
    sw.Stop();
    Console.WriteLine("string.format: " + sw.ElapsedTicks);
    
    sw.Reset();
    sw.Start();
    for (int i = 0; i < 10000; i++)
    {
        StringBuilder sb = new StringBuilder();
        string r = sb.AppendFormat("ABC{0}{1}{2}", i, i - 10,
            "dasdkadlkdjakdljadlkjdlkadjalkdj").ToString();
    }
    
    sw.Stop();
    Console.WriteLine("AppendFormat: " + sw.ElapsedTicks);
    
    0 讨论(0)
  • 2020-12-06 01:20

    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.

    0 讨论(0)
  • 2020-12-06 01:21

    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.

    0 讨论(0)
  • 2020-12-06 01:23

    @ Jerod Houghtelling Answer

    Actually String.Format uses a StringBuilder behind the scenes (use reflecton on String.Format if you want)

    I agree with the following answer in general

    0 讨论(0)
提交回复
热议问题