Most efficient way to concatenate strings?

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

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

17条回答
  •  栀梦
    栀梦 (楼主)
    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 });
    

提交回复
热议问题