Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format vs "string" + "string" or StringBuilder? [duplicate]

Tags:

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();
like image 215
Shimmy Weitzhandler Avatar asked May 28 '09 03:05

Shimmy Weitzhandler


People also ask

Is string format faster than StringBuilder?

StringBuilder is faster, because String. format has to parse the format string (a complex domain specific language).

Is it good to use string format?

tl;dr. Avoid using String. format() when possible. It is slow and difficult to read when you have more than two variables.

Is StringBuilder more efficient than string?

So from this benchmark test we can see that StringBuilder is the fastest in string manipulation. Next is StringBuffer , which is between two and three times slower than StringBuilder .

What is difference between string and StringBuilder?

StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are immutable but StringBuilder is the mutable string type. It will not create a new modified instance of the current string object but do the modifications in the existing string object.


1 Answers

  • 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 numeric) 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.
like image 104
Josh Avatar answered Sep 19 '22 03:09

Josh