.net String.Format vs "string" + "string" 还是 StringBuilder?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/919034/
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
String.Format vs "string" + "string" or StringBuilder?
提问by Shimmy Weitzhandler
Possible Duplicates:
Is String.Format as efficient as StringBuilder
C# String output: format or concat?
What is the performance priority and what should be the conditions to prefer each of the following:
什么是性能优先级以及优先选择以下各项的条件是什么:
String.Format("{0}, {1}", city, state);
or
或者
city + ", " + state;
or
或者
StringBuilder sb = new StringBuilder();
sb.Append(city);
sb.Append(", ");
sb.Append(state);
sb.ToString();
回答by Josh
- Compiler will optimize as much string
concatas it can, so for example strings that are just broken up for line break purposes can usually be optimized into a single string literal. - Concatenation with variables will get compiled into
String.Concat StringBuildercan be a lot faster if you're doing several (more than 10 or so I guess) "modifications" to a string but it carries some extra overhead because it allocates more space than you need in its buffer and resizes its internal buffer when it needs to.
- 编译器将尽可能多地优化字符串
concat,因此例如,为了换行而分解的字符串通常可以优化为单个字符串文字。 - 与变量的连接将被编译为
String.Concat StringBuilder如果您对字符串进行多次(我猜超过 10 次左右)“修改”,则速度可能会快得多,但它会带来一些额外的开销,因为它分配的空间比您在缓冲区中需要的空间多,并在它时调整其内部缓冲区的大小需要。
I personally use String.Formatalmost all of the time for two reasons:
我个人String.Format几乎所有时间都在使用,原因有两个:
- It's a lot easier to maintain the format string than rearranging a bunch of variables.
String.Formattakes aIFormatProviderwhich is passed to anyIFormattabletypes embedded in the string (such as numeric) so that you get appropriate numeric formatting for the specified cultureand overall just more control over how values are formatted.
- 维护格式字符串比重新排列一堆变量要容易得多。
String.Format将 aIFormatProvider传递给IFormattable嵌入在字符串中的任何类型(例如数字),以便您为指定的区域性获得适当的数字格式,并且总体上更好地控制值的格式。
For example, since some cultures use a comma as a decimal point you would want to ensure with either StringBuilderor String.Formatthat you specify CultureInfo.InvariantCultureif you wanted to ensure that numbers were formatted the way you intend.
例如,由于某些文化使用逗号作为小数点,因此您需要确保使用其中之一,StringBuilder或者String.Format指定CultureInfo.InvariantCulture是否要确保数字按预期方式格式化。
Two more thing to note...
还有两点要注意...
StringBuilderalso has anAppendFormatfunction which gives you the flexibility ofString.Formatwithout requiring an unnecessary second buffer.- When using
StringBuilder, make sure you don't defeat the purpose by concatenating parameters that you pass toAppend. It's an easy one to miss.
StringBuilder还具有一个AppendFormat功能,该功能为您提供了灵活性,String.Format而无需不必要的第二个缓冲区。- 使用 时
StringBuilder,请确保不会通过连接传递给 的参数来破坏目的Append。很容易错过。
回答by Eduardo Cobuci
There is no relevant diference. But assuming that String.Format internally uses a StringBuilder (you can see that with the Reflector tool), using directly StringBuilder.Append should be faster.
没有相关差异。但假设 String.Format 内部使用 StringBuilder(您可以使用 Reflector 工具看到),直接使用 StringBuilder.Append 应该会更快。
EDIT: Of course that use "+" operator is the worst option since it creates a new string instance for each string that you are concatenating.
编辑:当然,使用“+”运算符是最糟糕的选择,因为它为您要连接的每个字符串创建一个新的字符串实例。

