[ACCEPTED]-Convert string to array in without using Split function-string

Accepted answer
Score: 37

So you want an array of string, one char each:

string s = "abcdef";
string[] a = s.Select(c => c.ToString()).ToArray();

This 5 works because string implements IEnumerable<char>. So Select(c => c.ToString()) projects 4 each char in the string to a string representing that char and 3 ToArray enumerates the projection and converts 2 the result to an array of string.

If you're using 1 an older version of C#:

string s = "abcdef";
string[] a = new string[s.Length];
for(int i = 0; i < s.Length; i++) {
    a[i] = s[i].ToString();
Score: 9




Score: 3

You could use linq and do:

string value = "abcdef";
string[] letters = value.Select(c => c.ToString()).ToArray();

This would get 2 you an array of strings instead of an array 1 of chars.

Score: 3

Why don't you just

string value="abcd";



to show the first letter 2 of the string; that would be 'a' in this 1 case.

Score: 0

Bit more bulk than those above but i don't 2 see any simple one liner for this.

List<string> results = new List<string>; 

foreach(Char c in "abcdef".ToCharArray())

results.ToArray();  <-- done

What's 1 wrong with string.split???

More Related questions