IEnumerable<char> to string

Viewed 102761

I've never stumbled across this before, but I have now and am surprised that I can't find a really easy way to convert an IEnumerable<char> to a string.

The best way I can think of is string str = new string(myEnumerable.ToArray());, but, to me, it seems like this would create a new char[], and then create a new string from that, which seems expensive.

I would've thought this would be common functionality built into the .NET framework somewhere. Is there a simpler way to do this?

For those interested, the reason I'd like to use this is to use LINQ to filter strings:

string allowedString = new string(inputString.Where(c => allowedChars.Contains(c)).ToArray());
6 Answers
Related