How can I convert the IEnumerable<char>
"nonLetters" to a string[]
so that I can use it with String.Join?
string message = "This is a test message.";
var nonLetters = message.Where(x => !Char.IsLetter(x));
Console.WriteLine("There are {0} non-characters in \"{1}\" and they are: {2}",
nonLetters.Count(),
message,
String.Join(", ", nonLetters.ToArray())
);
string[] foo = nonLetters.Select(c => c.ToString()).ToArray();
If you don't actually care about using String.Join but only want the result, using new string(char[]) is the simplest change:
string message = "This is a test message.";
var nonLetters = message.Where(x => !Char.IsLetter(x));
Console.WriteLine("There are {0} non-characters in \"{1}\" and they are: {2}",
nonLetters.Count(),
message,
new string(nonLetters.ToArray()));
but for your example it is more efficient if you do it this way:
string message = "This is a test message.";
string nonLetters = new string(message.Where(x => !Char.IsLetter(x)).ToArray());
Console.WriteLine("There are {0} non-characters in \"{1}\" and they are: {2}",
nonLetters.Length,
message,
nonLetters);
The reason this is more efficient is that the other example iterates your where iterator twice: Once for the Count() call and the other time for the ToArray() call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With