using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { string astring = "Now is time for all good people"; ArrayList list_words = SplitWords(astring); foreach (string word in list_words) { Console.WriteLine(word); } Console.ReadKey(); } static ArrayList SplitWords(string s) { ArrayList words = new ArrayList(); int pos = s.IndexOf(" "); while (pos > 0) { string word = s.Substring(0, pos); words.Add(word); s = s.Substring(pos + 1, s.Length - (pos + 1)); pos = s.IndexOf(" "); if (pos == -1) { word = s.Substring(0,s.Length); words.Add(word); } } return words; } } }
也可以考虑封装的更友好,在SplitWords(string s)方法参数加一个传递分隔符数组,对数组里的每个分割符进行相应的分割功能。