Most efficient way to concatenate strings?

后端 未结 17 1217
野趣味
野趣味 2020-11-22 03:04

What\'s the most efficient way to concatenate strings?

相关标签:
17条回答
  • 2020-11-22 03:57

    The most efficient is to use StringBuilder, like so:

    StringBuilder sb = new StringBuilder();
    sb.Append("string1");
    sb.Append("string2");
    ...etc...
    String strResult = sb.ToString();
    

    @jonezy: String.Concat is fine if you have a couple of small things. But if you're concatenating megabytes of data, your program will likely tank.

    0 讨论(0)
  • 2020-11-22 03:57

    Another solution:

    inside the loop, use List instead of string.

    List<string> lst= new List<string>();
    
    for(int i=0; i<100000; i++){
        ...........
        lst.Add(...);
    }
    return String.Join("", lst.ToArray());;
    

    it is very very fast.

    0 讨论(0)
  • 2020-11-22 03:59

    Adding to the other answers, please keep in mind that StringBuilder can be told an initial amount of memory to allocate.

    The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. Its value is assigned to the Capacity property. If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.

    If capacity is zero, the implementation-specific default capacity is used.

    Repeatedly appending to a StringBuilder that hasn't been pre-allocated can result in a lot of unnecessary allocations just like repeatedly concatenating regular strings.

    If you know how long the final string will be, can trivially calculate it, or can make an educated guess about the common case (allocating too much isn't necessarily a bad thing), you should be providing this information to the constructor or the Capacity property. Especially when running performance tests to compare StringBuilder with other methods like String.Concat, which do the same thing internally. Any test you see online which doesn't include StringBuilder pre-allocation in its comparisons is wrong.

    If you can't make any kind of guess about the size, you're probably writing a utility function which should have its own optional argument for controlling pre-allocation.

    0 讨论(0)
  • 2020-11-22 04:00

    The StringBuilder.Append() method is much better than using the + operator. But I've found that, when executing 1000 concatenations or less, String.Join() is even more efficient than StringBuilder.

    StringBuilder sb = new StringBuilder();
    sb.Append(someString);
    

    The only problem with String.Join is that you have to concatenate the strings with a common delimiter.

    Edit: as @ryanversaw pointed out, you can make the delimiter string.Empty.

    string key = String.Join("_", new String[] 
    { "Customers_Contacts", customerID, database, SessionID });
    
    0 讨论(0)
  • 2020-11-22 04:00

    There are 6 types of string concatenations:

    1. Using the plus (+) symbol.
    2. Using string.Concat().
    3. Using string.Join().
    4. Using string.Format().
    5. Using string.Append().
    6. Using StringBuilder.

    In an experiment, it has been proved that string.Concat() is the best way to approach if the words are less than 1000(approximately) and if the words are more than 1000 then StringBuilder should be used.

    For more information, check this site.

    string.Join() vs string.Concat()

    The string.Concat method here is equivalent to the string.Join method invocation with an empty separator. Appending an empty string is fast, but not doing so is even faster, so the string.Concat method would be superior here.

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