Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

string.Fromat vs StringBuilder vs +

Which is more efficient:

1. string.Fromat("some text {0}", text)
2. Stringbuilder
3. concatinate using +
0
gustierng
Asked:
gustierng
1 Solution
 
amxCommented:
Stringbuilder is most efficient then String.format and finally +

Why is stringbuilder more efficient than string.format? Its because string.format actually uses stringBuilder internally and the applies the formatting.

+ is the slowest as string is immutable, which means you have to create a new string object each time you change the value of a string variable

0
 
Anurag ThakurCommented:
+ will be the slowest operation and mentioned by amx and to further add on to what amx has to say please go through the following link
http://www.dotnetjunkies.com/WebLog/tituswave/archive/2005/04/09/63241.aspx
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
drichardsCommented:
The comparison of methods depends on the situation.  It would be inaccurate to make such generalizations as "StringBuilder is fastest" and "+ operator is slowest".

When you need to build a long string with multiple operations, this is certainly true.  If the desire is siimply to concatenate two existing strings, the + operator will be just as good, and potentially better.  If the desired result is something that can be accomplished by a single call to String.Format, then that will be just as efficient as Stringbuilder.  Since the Format method tries to make a guess as to the length of your result whereas the average programmer uses default construction, Format may actually outperform Stringbuilder.

Bottom line is that you need to take into account your specific programming need before you can say which method is going to be most efficient.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now