6

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>.

6 Answers 6

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();
Sign up to request clarification or add additional context in comments.

Comments

2

try this

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

3 Comments

@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
thank you @Ripple and knittl for being fair , I was mistaken in getting the question in hurry and I'm sorry for that
@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.
1

Try following

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

Comments

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());

Comments

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

Comments

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());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.