Best way to convert IEnumerable<char> to string?
How about this to convert IEnumerable<char>
to string
:
string.Concat(x.TakeWhile(char.IsLetter));
Edited for the release of .Net Core 2.1
Repeating the test for the release of .Net Core 2.1, I get results like this
1000000 iterations of "Concat" took 842ms.
1000000 iterations of "new String" took 1009ms.
1000000 iterations of "sb" took 902ms.
In short, if you are using .Net Core 2.1 or later, Concat
is king.
See MS blog post for more details.
I've made this the subject of another question but more and more, that is becoming a direct answer to this question.
I've done some performance testing of 3 simple methods of converting an IEnumerable<char>
to a string
, those methods are
new string
return new string(charSequence.ToArray());
Concat
return string.Concat(charSequence)
StringBuilder
var sb = new StringBuilder();
foreach (var c in charSequence)
{
sb.Append(c);
}
return sb.ToString();
In my testing, that is detailed in the linked question, for 1000000
iterations of "Some reasonably small test data"
I get results like this,
1000000 iterations of "Concat" took 1597ms.
1000000 iterations of "new string" took 869ms.
1000000 iterations of "StringBuilder" took 748ms.
This suggests to me that there is not good reason to use string.Concat
for this task. If you want simplicity use the new string approach and if want performance use the StringBuilder.
I would caveat my assertion, in practice all these methods work fine, and this could all be over optimization.
Assuming that you're looking predominantly for performance, then something like this should be substantially faster than any of your examples:
string x = "asdf1234";
string y = x.LeadingLettersOnly();
// ...
public static class StringExtensions
{
public static string LeadingLettersOnly(this string source)
{
if (source == null)
throw new ArgumentNullException("source");
if (source.Length == 0)
return source;
char[] buffer = new char[source.Length];
int bufferIndex = 0;
for (int sourceIndex = 0; sourceIndex < source.Length; sourceIndex++)
{
char c = source[sourceIndex];
if (!char.IsLetter(c))
break;
buffer[bufferIndex++] = c;
}
return new string(buffer, 0, bufferIndex);
}
}