I have text. for example string text = "COMPUTER"
And I want to split it into characters, to keep every character as string.
If there were any delimiter I can use text.Split(delimiter).
But there is not any delimiter, I convert it to char array withtext.ToCharArray().toList().
And after that I get List<char>. But I need List<string>.
So How can I convert List<char> to List<string>.
Just iterate over the collection of characters, and convert each to a string:
var result = input.ToCharArray().Select(c => c.ToString()).ToList();
Or shorter (and more efficient, since we're not creating an extra array in between):
var result = input.Select(c => c.ToString()).ToList();
try this
var result = input.Select(c => c.ToString()).ToList();
Try following
string text = "COMPUTER"
var listOfChars = text.Select(x=>new String(new char[]{x})).ToArray()
Use the fact that a string is internally already very close to an char[]
Approach without LINQ:
List<string> list = new List<string();
for(int i = 0; i < s.Length; i++)
list.Add(s[i].ToString());
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