连接字符串的最有效方法? [英] Most efficient way to concatenate strings?

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

问题描述

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

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 executing 1000 concatenations or less, 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.

,如 @ryanversaw 所指出的,您可以将定界符设置为string.Empty.

as @ryanversaw pointed out, you can make the delimiter string.Empty.

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

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

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