Most efficent way of joining strings

前端 未结 11 1733
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 05:51

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\"
\"1232131         


        
11条回答
  •  迷失自我
    2021-01-18 06:30

    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();
    

提交回复
热议问题