Most efficent way of joining strings
Solution 1
As @Ekkehard said, use the string.Join.
However, you do not need the ToArray()
because string.Join
has an overload for IEnumerable<string>
.
List<string> stringList = new List<string>
{ "1234567890", "34343434", "4343434" };
string outcome = string.Join(",", stringList);
EDIT
As @Kobi said, this will work only C# 4.0. In 3.5 I would do.
var s = new StringBuilder(stringList.Count * 8);
foreach (var item in stringList)
{
s.Append(item);
s.Append(',');
}
s.Length -= 1;
string outcome = stringList.ToList();
Solution 2
You should use string.Join()
because:
a) it's much more readable, maintainable and easy on the eyes.
b) it uses a StringBuilder
internally already, so it's very efficient ( you can confirm yourself using Reflector).
Edit:
string.Join()
uses a StringBuilder
for the general case of an IEnumerable<T>
input. If you already have an array on the other hand it uses some voodoo magic (including FastAllocateString()
and UnSafeCharBuffer
) to be even faster.
Solution 3
Use Join, because it doesn't add a trailing ",".
Solution 4
There's a benchmark on this page that seems to show that string.Join
performs better than StringBuilder
on a small array in a lot of iterations. you should probably benchmark for larger arrays also. As I'm posting this I see that BrokenGlass answered that StringBuilder
is used internally in string.Join
so you can expect it to be faster I guess.
Solution 5
Your second solution adds an extra ,
at the end. Take a look at Eric Lippert`s blog entry
I would recommend fixing your second solution. A StringBuilder would be definitely faster, as you avoid coping the list contents to a new array.
StringBuilder builder = new StringBuilder();
string separator = "";
stringList.ForEach(
val =>
{
builder.Append(separator).Append(val);
separator = ",";
});
string outcome = builder.ToString();
image
Updated on June 12, 2022Comments
-
image almost 2 years
I need to concatenate a lot of strings alltogether and put a comma between any of them. I have a list of strings
"123123123213" "1232113213213" "123213123"
and I want to get
"123123123213,1232113213213,123213123"
I was wondering what is the best way to achieve that.
I could do this like this:
private List<string> stringList = new List<string> { // a lot of strings in here "1234567890", "34343434", "4343434" }; string outcome = string.Join(",", stringList.ToArray());
Or maybe:
StringBuilder builder = new StringBuilder(); stringList.ForEach(val => { builder.Append(val); builder.Append(","); }); string outcome = builder.ToString();
Which way is better? Do you know better ways to concatenate strings?
-
Kobi almost 13 yearsNote that this is new in .Net 4, 3.5 still needs an array.
-
svick almost 13 yearsExcept
string.Join()
can be even more effective (at least in the case, when you give itstring[]
, otherwise is usesStringBuilder
itself). -
Marino Šimić almost 13 yearssvick you have given a -1 to somebody that said something more clever than you. And however the asker needs it for a list of strings not for an array.
-
svick almost 13 yearsFYI,
string.Join(string, string[])
does not useStringBuilder
, butUnSafeCharBuffer
. -
svick almost 13 years@Marino, how is using
StringBuilder
more clever than usingstring.Join()
? The latter is much more clearer, less error prone (you don't have to write your own code) and possibly faster. -
BrokenGlass almost 13 years@svick: Yes just was in the process of editing that in after making that observation as well- it's vodoo magic indeed ;-)
-
Kevin Hsu almost 13 yearsGosh, it is just an alternative.
-
Kevin Hsu almost 13 yearsTo use string.join you have to take the list and first make an array. That may not be the most efficient way, especially if you have a lot of little strings.