Studyzy.LearnEnglishBySubtitle.SentenceParse.SplitSentence C# (CSharp) Method

SplitSentence() public static method

将一个句子分成单词和符号(空格)组成的数组
public static SplitSentence ( string sentence ) : ICollection
sentence string
return ICollection
        public static ICollection<string> SplitSentence(string sentence)
        {
            List<string> result=new List<string>();
            StringBuilder sb=new StringBuilder();
            foreach (char c in sentence)
            {
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')||c=='-')
                {
                    sb.Append(c);
                }
                else
                {
                    string word = sb.ToString();
                    if (word != "")
                    {
                        result.Add(word);
                    }
                    sb.Clear();
                    result.Add(c.ToString());
                }
            }
            var last = sb.ToString();
            if (last != "")
            {
                result.Add(last);
            }
            return result;
        }