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>
.
Asked
Active
Viewed 2.2k times
6

namco
- 6,208
- 20
- 59
- 83
6 Answers
16
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
- 65,241
- 13
- 115
- 165
2
try this
var result = input.Select(c => c.ToString()).ToList();

varsha
- 1,620
- 1
- 16
- 29
-
2@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
- 30,108
- 19
- 83
- 131
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,556
- 2
- 34
- 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,752
- 8
- 42
- 63
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());
}

MrCool4000
- 5
- 1
- 4