views:

5068

answers:

12

What's the most efficient way to concatenate strings?

A: 

stringbuilder

SQLMenace
lacks explanation
mafutrct
+3  A: 

String.Concat(str1, str2) ?

jonezy
+2  A: 

The most efficient is to use StringBuilder, like so:

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.

DannySmurf
+15  A: 

The StringBuilder.Append() method is much better than using the + operator. But I've found that, when the concatenations are less than 1000, String.Join() is even more efficient than 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 key = String.Join("_", new String[] 
{ "Customers_Contacts", customerID, database, SessionID });
TheImirOfGroofunkistan
Isn't the 'ToString' call on 'someString' unnecessary? Unless 'someString' is really an int, and it's a big lying liar. ;)
Sarah Vessels
funny, but good point :)
TheImirOfGroofunkistan
It would be good to note that though String.Join adds a delimiter, you can make that delimiter String.Empty.
Ryan Versaw
`StringBuilder` has a huge comparable start-up cost, it's only efficient when used with very large strings, or very many concatenations. It isn't trivial to find out for any given situation. If performance is of issue, profiling is your friend (check ANTS).
Abel
A: 

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.

So, for more that 2-3 strings, use DannySmurf's code. Otherwise, just use the + operator.

Nick
+12  A: 

From Chinh Do - StringBuilder is not always faster:

Rules of Thumb

  • When concatenating three dynamic string values or less, use traditional string concatenation.

  • When concatenating more than three dynamic string values, use StringBuilder.

  • When building a big string from several string literals, use either the @ string literal or the inline + operator.

Most of 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.

palehorse
afaik @ only turns off escape sequences processing. http://msdn.microsoft.com/en-us/library/362314fe.aspx agree
abatishchev
+1  A: 

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.

Jon Dewees
+3  A: 

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.

However, Rico Mariani (.NET optimization guru) made up a quiz in which he stated at the end that, in most cases, he recommends String.Format.

Adam V
I've been recommending the use of string.format over string + string for years to people I've worked with. I think the readability advantages are an additional advantage beyond the performance benefit.
Scott A. Lawrence
A: 

There's a similar question here that covered the performance of string concatenation methods.

Greg
+19  A: 

Rico Mariani, the .NET Performance guru, had an article on this very subject. It's not as simple as one might suspect. The basic advice is this:

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.

Lee
+1  A: 

MS Support Link:

How to improve string concatenation performance in Visual C#

James D
+2  A: 

From this MSDN article:

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.

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.

Simple and sound, if - I repeat - you decide to trust MSDN.

JohnIdol