Most efficent way of joining strings

13,410

Solution 1

As @Ekkehard said, use the string.Join.

However, you do not need the ToArray() because string.Join has an overload for IEnumerable<string>.

List<string> stringList = new List<string> 
    { "1234567890", "34343434", "4343434" }; 

string outcome = string.Join(",", stringList);

EDIT

As @Kobi said, this will work only C# 4.0. In 3.5 I would do.

var s = new StringBuilder(stringList.Count * 8);
foreach (var item in stringList)
{
   s.Append(item);
   s.Append(',');
}
s.Length -= 1;
string outcome = stringList.ToList();

Solution 2

You should use string.Join() because:

a) it's much more readable, maintainable and easy on the eyes.

b) it uses a StringBuilder internally already, so it's very efficient ( you can confirm yourself using Reflector).

Edit:

string.Join() uses a StringBuilder for the general case of an IEnumerable<T> input. If you already have an array on the other hand it uses some voodoo magic (including FastAllocateString() and UnSafeCharBuffer) to be even faster.

Solution 3

Use Join, because it doesn't add a trailing ",".

Solution 4

There's a benchmark on this page that seems to show that string.Join performs better than StringBuilder on a small array in a lot of iterations. you should probably benchmark for larger arrays also. As I'm posting this I see that BrokenGlass answered that StringBuilder is used internally in string.Join so you can expect it to be faster I guess.

Solution 5

Your second solution adds an extra , at the end. Take a look at Eric Lippert`s blog entry

I would recommend fixing your second solution. A StringBuilder would be definitely faster, as you avoid coping the list contents to a new array.

StringBuilder builder = new StringBuilder();
string separator = "";
stringList.ForEach(
    val =>
    {
        builder.Append(separator).Append(val);
        separator = ",";
    });
string outcome = builder.ToString();
Share:
13,410
image
Author by

image

Updated on June 12, 2022

Comments

  • image
    image almost 2 years

    I need to concatenate a lot of strings alltogether and put a comma between any of them. I have a list of strings

    "123123123213"
    "1232113213213"
    "123213123"
    

    and I want to get

    "123123123213,1232113213213,123213123"
    

    I was wondering what is the best way to achieve that.

    I could do this like this:

    private List<string> stringList = new List<string> { 
        // a lot of strings in here
        "1234567890", "34343434", "4343434" }; 
    
    string outcome = string.Join(",", stringList.ToArray());
    

    Or maybe:

    StringBuilder builder = new StringBuilder();
    stringList.ForEach(val => {
        builder.Append(val);
        builder.Append(",");
    });
    
    string outcome = builder.ToString();
    

    Which way is better? Do you know better ways to concatenate strings?

  • Kobi
    Kobi almost 13 years
    Note that this is new in .Net 4, 3.5 still needs an array.
  • svick
    svick almost 13 years
    Except string.Join() can be even more effective (at least in the case, when you give it string[], otherwise is uses StringBuilder itself).
  • Marino Šimić
    Marino Šimić almost 13 years
    svick you have given a -1 to somebody that said something more clever than you. And however the asker needs it for a list of strings not for an array.
  • svick
    svick almost 13 years
    FYI, string.Join(string, string[]) does not use StringBuilder, but UnSafeCharBuffer.
  • svick
    svick almost 13 years
    @Marino, how is using StringBuilder more clever than using string.Join()? The latter is much more clearer, less error prone (you don't have to write your own code) and possibly faster.
  • BrokenGlass
    BrokenGlass almost 13 years
    @svick: Yes just was in the process of editing that in after making that observation as well- it's vodoo magic indeed ;-)
  • Kevin Hsu
    Kevin Hsu almost 13 years
    Gosh, it is just an alternative.
  • Kevin Hsu
    Kevin Hsu almost 13 years
    To use string.join you have to take the list and first make an array. That may not be the most efficient way, especially if you have a lot of little strings.