的String.Format VS"串" +"串"或StringBuilder的? [英] String.Format vs "string" + "string" or StringBuilder?

查看:203
本文介绍了的String.Format VS"串" +"串"或StringBuilder的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能显示的文件:
  是的String.Format同样有效的StringBuilder
   C#字符串输出:格式或CONCAT

什么是效率优先,并应该是什么条件,preFER每个如下:

 的String.Format({0},{1},市,州);
 

 城市+,+状态;
 

  StringBuilder的SB =新的StringBuilder();
sb.Append(市);
sb.Append(,);
sb.Append(州);
sb.ToString();
 

解决方案
  • 在编译器将优化尽可能多的字符串连接,因为它可以,因此,例如字符串,只是分解为断行的目的通常可以优化成一个字符串。
  • 在串联与变量将被编译成String.Concat
  • 在StringBuilder的可快了很多,如果你正在做一些(超过10个,所以我猜)修改,以一个字符串,但它会带来一些额外的开销,因为它分配更多的空间比你需要在它的缓冲区,并重新调整其当它需要内部缓冲区。

我个人使用的String.Format几乎所有的时间,原因有二:

  • 这是一个更容易维护格式字符串比重新安排了一些变量。
  • 的String.Format需要被传递给任何IFormattable类型的嵌入字符串(如数字),以便您获得特定的文化和整体只是更好地控制值的格式适当的数字格式化的IFormatProvider。

例如,因为有些文化中使用逗号作为小数点你想确保与任何StringBuilder的或的String.Format您指定CultureInfo.InvariantCulture如果你想确保数字被格式化你想让你的方式。

两件事要注意...

  • 在StringBuilder的也有一个AppendFormat功能,让你的String.Format的灵活性,而无需不必要的第二个缓冲区。
  • 在使用StringBuilder的,请务必不要通过将您传递给附加参数击败目的。这是一个容易错过。

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();

解决方案

  • Compiler will optimize as much string concat as 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
  • StringBuilder can 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.

I personally use String.Format almost all of the time for two reasons:

  • It's a lot easier to maintain the format string than rearranging a bunch of variables.
  • String.Format takes a IFormatProvider which is passed to any IFormattable types embedded in the string (such as numerics) so that you get appropriate numeric formatting for the specified culture and overall just more control over how values are formatted.

For example, since some cultures use a comma as a decimal point you would want to ensure with either StringBuilder or String.Format that you specify CultureInfo.InvariantCulture if you wanted to ensure that numbers were formatted the way you intend.

Two more thing to note...

  • StringBuilder also has an AppendFormat function which gives you the flexibility of String.Format without requiring an unnecessary second buffer.
  • When using StringBuilder, make sure you don't defeat the purpose by concatenating parameters that you pass to Append. It's an easy one to miss.

这篇关于的String.Format VS"串" +"串"或StringBuilder的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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