How to convert List <char> to List <string> in C #?

I have a text. e.g. string text = "COMPUTER"
And I want to break it into characters so that each character is like a string.
If there was any delimiter I could use text.Split(delimiter) .
But there is no separator, I convert it to a char array using text.ToCharArray().toList() .
And after that I get a List<char> . But I need a List<string> .
So how can I convert a List<char> to a List<string> .

+5
source share
6 answers

Just iterate over the character set and convert them to a string:

 var result = input.ToCharArray().Select(c => c.ToString()).ToList(); 

Or shorter (and more efficient, since we do not create an extra array between them):

 var result = input.Select(c => c.ToString()).ToList(); 
+11
source

try it

  var result = input.Select(c => c.ToString()).ToList(); 
+2
source

Try to execute

 string text = "COMPUTER" var listOfChars = text.Select(x=>new String(new char[]{x})).ToArray() 
+1
source

Use the fact that a string internally very close to char[]

Non-LINQ Approach:

 List<string> list = new List<string(); for(int i = 0; i < s.Length; i++) list.Add(s[i].ToString()); 
+1
source
  string bla = "COMPUTER"; //Your String List<char> list = bla.ToCharArray().ToList(); //Your char list List<string> otherList = new List<string>(); //Your string list list.ForEach(c => otherList.Add(c.ToString())); //iterate through char-list convert every char to string and add it to your string list 
+1
source

Use this:

Key:

charList - your character list

strList is your list of strings

the code:

 List<string> strList = new List<string>(); foreach(char x in charList) { strList.Add(x.ToString()); } 
0
source

Source: https://habr.com/ru/post/1211225/


All Articles