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?
The comma “,” is passed to the explode() function as a delimiter to convert the string into array elements.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
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();
}
Yes.
"abcdef".ToCharArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With