什么是用C#最好的字符串连接方法是什么? [英] What's the best string concatenation method using C#?

查看:159
本文介绍了什么是用C#最好的字符串连接方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是连接字符串的最有效方法是什么?

What's the most efficient way to concatenate strings?

推荐答案

StringBuilder.Append()方法比使用+操作要好得多。但我发现,当串联低于1000,的string.join()甚至比更有效的StringBuilder

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

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

的string.join 唯一的问题是,你必须在连接字符串与常用的分隔符。

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

string key = String.Join("_", new String[] 
{ "Customers_Contacts", customerID, database, SessionID });

这篇关于什么是用C#最好的字符串连接方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆