实现标题的功能,方法也很多。Insus.NET也为此练习一番。
如有字符串:
string str = "Insus.NET";
方法一:
public void SplitString(string str) { int len = str.Length; for (int i = 0; i < len; i++) { Console.WriteLine(str[i]); } }Source Code
方法二:
DBA3AF.jpg" alt="" />
public void SplitString0(string str) { string[] arr = new string[str.Length]; for (int i = 0; i < str.Length; i++) { arr[i] = str[i].ToString(); } foreach (string s in arr) { Console.WriteLine(s); } }Source Code
方法三:
public void SplitString1(string str) { foreach (Char c in str.ToCharArray()) { Console.WriteLine(c); } }Source Code
方法四:
public void SplitString2(string str) { string[] arr = str.Select(s => s.ToString()).ToArray(); foreach (string s in arr) { Console.WriteLine(s); } }Source Code
不管哪一种方法,输出结果: