Most efficent way of joining strings

前端 未结 11 1753
佛祖请我去吃肉
佛祖请我去吃肉 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:34

    String.Join is the fastest if you have a fixed number of strings to join. The deeper reason is that String.Join does loop over the array and allocates the final string buffer with the right size because it does in the first pass add the string length of all strings together. You can get similar results when you play around with StringBuilder and its capacity. The golden rule is to spare memory allocations at the cost of looping over the array two times. Which approach is faster depends on how many items you have in your list and how big the resultint string will get.

    Yours, Alois Kraus

提交回复
热议问题