首先,设置一个字符串-
string str = "Science and Mathematics";
现在使用该Split()
方法拆分出现空格的位置-
str.Split(' ')
以下是完整的代码-
using System; using System.Linq; using System.IO; class Program { static void Main() { string str = "Science and Mathematics"; Console.WriteLine("String...\n"+str); string[] myStr = str.Split(' '); Console.WriteLine("\nSplitted String..."); foreach (string ch in myStr) { Console.WriteLine(ch); } } }
输出结果
String... Science and Mathematics Splitted String... Science and Mathematics