Why isn\'t it possible to use fluent language on string?
For example:
var x = \"asdf1234\";
var y = new string(x.TakeWhile(char.IsLetter
Why isn't it possible to use fluent language on string?
It is possible. You did it in the question itself:
var y = new string(x.TakeWhile(char.IsLetter).ToArray());
Isn't there a better way to convert
IEnumerableto string?
(My assumption is:)
The framework does not have such a constructor because strings are immutable, and you'd have to traverse the enumeration twice in order to pre-allocate the memory for the string. This is not always an option, especially if your input is a stream.
The only solution to this is to push to a backing array or StringBuilder first, and reallocate as the input grows. For something as low-level as a string, this probably should be considered too-hidden a mechanism. It also would push perf problems down into the string class by encouraging people to use a mechanism that cannot be as-fast-as-possible.
These problems are solved easily by requiring the user to use the ToArray extension method.
As others have pointed out, you can achieve what you want (perf and expressive code) if you write support code, and wrap that support code in an extension method to get a clean interface.