Convert String to Array in Without Using Split Function

Is there any way to convert a string ("abcdef") to an array of string containing its character (["a","b","c","d","e","f"]) without using the String.Split function?

5 Answers

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

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

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

If you're using 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();
}
2

Yes.

"abcdef".ToCharArray();
2

You could use linq and do:

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

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

Why don't you just

string value="abcd";

value.ToCharArray();

textbox1.Text=Convert.toString(value[0]);

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

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

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

foreach(Char c in "abcdef".ToCharArray())
{
   results.add(c.ToString());
}


results.ToArray();  <-- done

What's wrong with string.split???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest