[ACCEPTED]-Convert string to array in without using Split function-string
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();
}
Yes.
"abcdef".ToCharArray();
0
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.
Why don't you just
string value="abcd";
value.ToCharArray();
textbox1.Text=Convert.toString(value[0]);
to show the first letter 2 of the string; that would be 'a' in this 1 case.
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.add(c.ToString());
}
results.ToArray(); <-- done
What's 1 wrong with string.split???
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.