字符串转换为数组中不使用Split函数 [英] Convert string to array in without using Split function
问题描述
有没有什么办法将一个字符串(ABCDEF
)转化为含有其性格( [字符串数组 b,C,D,E,F]
),而不使用 String.Split
功能?
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?
推荐答案
所以,你要字符串
的数组,其中字符
每个:
So you want an array of string
, one char
each:
string s = "abcdef";
string[] a = s.Select(c => c.ToString()).ToArray();
这工作,因为字符串
工具的IEnumerable<焦炭>
。因此,选择(C => c.ToString())
项目每个字符
在字符串
到字符串
重新presenting的字符
和 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
.
如果您正在使用C#的是旧版本:
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();
}
这篇关于字符串转换为数组中不使用Split函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!