5

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 with
text.ToCharArray().toList().
And after that I get List<char>. But I need List<string>.
So How can I convert List<char> to List<string>.

namco
  • 5,912
  • 17
  • 57
  • 83

6 Answers6

14

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();
Grant Winney
  • 63,310
  • 12
  • 110
  • 157
2

try this

   var result = input.Select(c => c.ToString()).ToList();
varsha
  • 1,624
  • 1
  • 15
  • 29
  • 1
    @Ripple: because the answer has been edited since (but still within the initial "historyless" time window). The original answer had nothing to do with the question – knittl Jan 16 '15 at 07:04
  • thank you @Ripple and knittl for being fair , I was mistaken in getting the question in hurry and I'm sorry for that – varsha Jan 16 '15 at 07:17
  • @Ripple: I cannot remove the upvote, because the edit does not show up as edit. Upvotes can only be removed after a (real) edit was made. – knittl Jan 16 '15 at 07:39
1

Try following

string text = "COMPUTER"
var listOfChars = text.Select(x=>new String(new char[]{x})).ToArray()
Tilak
  • 28,854
  • 17
  • 79
  • 129
1

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());
DrKoch
  • 9,212
  • 2
  • 31
  • 43
1
    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
チーズパン
  • 2,710
  • 8
  • 40
  • 60
0

Use this:

Key:

charList is your Character List

strList is your String List

Code:

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