C# 连接字符串的最有效方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most efficient way to concatenate strings?
提问by jimmij
What's the most efficient way to concatenate strings?
连接字符串的最有效方法是什么?
采纳答案by TheEmirOfGroofunkistan
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.Append()
方法比使用+
运算符好得多。但我发现,当执行 1000 次或更少的连接时,String.Join()
甚至比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.
唯一的问题String.Join
是您必须使用公共分隔符连接字符串。
Edit:as @ryanversawpointed out, you can make the delimiter string.Empty
.
编辑:正如@ryanversaw指出的那样,您可以制作定界符string.Empty
。
string key = String.Join("_", new String[]
{ "Customers_Contacts", customerID, database, SessionID });
回答by TheSmurf
The most efficient is to use StringBuilder, like so:
最有效的是使用 StringBuilder,如下所示:
StringBuilder sb = new StringBuilder();
sb.Append("string1");
sb.Append("string2");
...etc...
String strResult = sb.ToString();
@jonezy: String.Concat is fine if you have a couple of small things. But if you're concatenating megabytes of data, your program will likely tank.
@jonezy:如果你有一些小东西,String.Concat 很好。但是,如果您连接数兆字节的数据,您的程序可能会崩溃。
回答by Nick
For just two strings, you definitely do not want to use StringBuilder. There is some threshold above which the StringBuilder overhead is less than the overhead of allocating multiple strings.
对于仅两个字符串,您绝对不想使用 StringBuilder。存在一些阈值,高于该阈值 StringBuilder 的开销小于分配多个字符串的开销。
So, for more that 2-3 strings, use DannySmurf's code. Otherwise, just use the + operator.
因此,对于超过 2-3 个字符串,请使用DannySmurf 的代码。否则,只需使用 + 运算符。
回答by palehorse
From Chinh Do - StringBuilder is not always faster:
来自Chinh Do - StringBuilder 并不总是更快:
Rules of Thumb
经验法则
When concatenating threedynamic string values or less, use traditional string concatenation.
When concatenating more than threedynamic string values, use
StringBuilder
.When building a big string from several string literals, use either the
@
string literal or the inline + operator.
连接三个或更少的动态字符串值时,请使用传统的字符串连接。
连接三个以上的动态字符串值时,请使用
StringBuilder
.从多个字符串文字构建大字符串时,请使用
@
字符串文字或内联 + 运算符。
Mostof the time StringBuilder
is your best bet, but there are cases as shown in that post that you should at least think about each situation.
大多数情况下StringBuilder
是您最好的选择,但在该帖子中显示的某些情况下,您至少应该考虑每种情况。
回答by Jon Dewees
It would depend on the code. StringBuilder is more efficient generally, but if you're only concatenating a few strings and doing it all in one line, code optimizations will likely take care of it for you. It's important to think about how the code looks too: for larger sets StringBuilder will make it easier to read, for small ones StringBuilder will just add needless clutter.
这将取决于代码。StringBuilder 通常更有效,但如果您只连接几个字符串并在一行中完成所有操作,代码优化可能会为您处理。考虑代码的外观也很重要:对于较大的集合,StringBuilder 将使其更易于阅读,对于较小的集合,StringBuilder 只会增加不必要的混乱。
回答by Adam V
If you're operating in a loop, StringBuilder
is probably the way to go; it saves you the overhead of creating new strings regularly. In code that'll only run once, though, String.Concat
is probably fine.
如果您在循环中操作,StringBuilder
可能是要走的路;它为您节省了定期创建新字符串的开销。不过,在只运行一次的代码中,String.Concat
可能没问题。
However, Rico Mariani (.NET optimization guru) made up a quizin which he stated at the end that, in most cases, he recommends String.Format
.
然而,Rico Mariani(.NET 优化大师)做了一个测验,他在最后说,在大多数情况下,他推荐String.Format
.
回答by Lee
Rico Mariani, the .NET Performance guru, had an articleon this very subject. It's not as simple as one might suspect. The basic advice is this:
.NET 性能大师Rico Mariani有一篇关于这个主题的文章。这并不像人们想象的那么简单。基本建议是这样的:
If your pattern looks like:
x = f1(...) + f2(...) + f3(...) + f4(...)
that's one concat and it's zippy, StringBuilder probably won't help.
If your pattern looks like:
if (...) x += f1(...)
if (...) x += f2(...)
if (...) x += f3(...)
if (...) x += f4(...)
then you probably want StringBuilder.
如果您的模式如下所示:
x = f1(...) + f2(...) + f3(...) + f4(...)
那是一个 concat 并且它很活泼,StringBuilder 可能无济于事。
如果您的模式如下所示:
if (...) x += f1(...)
if (...) x += f2(...)
if (...) x += f3(...)
if (...) x += f4(...)
那么你可能想要 StringBuilder。
Yet another article to support this claimcomes from Eric Lippert where he describes the optimizations performed on one line +
concatenations in a detailed manner.
支持这一说法的另一篇文章来自 Eric Lippert,他在其中+
详细描述了对一行连接执行的优化。
回答by JohnIdol
From this MSDN article:
来自这篇MSDN 文章:
There is some overhead associated with creating a StringBuilder object, both in time and memory. On a machine with fast memory, a StringBuilder becomes worthwhile if you're doing about five operations. As a rule of thumb, I would say 10 or more string operations is a justification for the overhead on any machine, even a slower one.
创建 StringBuilder 对象会产生一些开销,包括时间和内存方面的开销。在具有快速内存的机器上,如果您执行大约五次操作,则 StringBuilder 变得值得。根据经验,我会说 10 次或更多字符串操作是任何机器上开销的理由,即使是较慢的机器。
So if you trust MSDN go with StringBuilder if you have to do more than 10 strings operations/concatenations - otherwise simple string concat with '+' is fine.
因此,如果您信任 MSDN,如果您必须执行 10 个以上的字符串操作/连接,请使用 StringBuilder - 否则使用 '+' 的简单字符串连接就可以了。
回答by Liran
It really depends on your usage pattern. A detailed benchmark between string.Join, string,Concat and string.Format can be found here: String.Format Isn't Suitable for Intensive Logging
这实际上取决于您的使用模式。可以在此处找到 string.Join、string、Concat 和 string.Format 之间的详细基准测试:String.Format 不适合密集日志记录
(This is actually the same answer I gave to thisquestion)
(这实际上是我对这个问题给出的相同答案)
回答by Mr_Green
There are 6 types of string concatenations:
有 6 种类型的字符串连接:
- Using the plus (
+
) symbol. - Using
string.Concat()
. - Using
string.Join()
. - Using
string.Format()
. - Using
string.Append()
. - Using
StringBuilder
.
- 使用加号 (
+
) 符号。 - 使用
string.Concat()
. - 使用
string.Join()
. - 使用
string.Format()
. - 使用
string.Append()
. - 使用
StringBuilder
.
In an experiment, it has been proved that string.Concat()
is the best way to approach if the words are less than 1000(approximately) and if the words are more than 1000 then StringBuilder
should be used.
在一个实验中,已经证明string.Concat()
如果单词小于1000(大约)是最好的方法,如果单词大于1000则StringBuilder
应该使用。
For more information, check this site.
有关更多信息,请查看此站点。
string.Join() vs string.Concat()
The string.Concat method here is equivalent to the string.Join method invocation with an empty separator. Appending an empty string is fast, but not doing so is even faster, so the string.Concatmethod would be superior here.
string.Join() 与 string.Concat()
此处的 string.Concat 方法等效于带有空分隔符的 string.Join 方法调用。附加一个空字符串很快,但不这样做会更快,所以string.Concat方法在这里会更好。