How to split text into words?

前端 未结 7 2354
-上瘾入骨i
-上瘾入骨i 2020-12-04 22:39

How to split text into words?

Example text:

\'Oh, you can\'t help that,\' said the Cat: \'we\'re all mad here. I\'m mad. You\'re mad.\'

7条回答
  •  情话喂你
    2020-12-04 22:58

    This is one of solution, i dont use any helper class or method.

            public static List ExtractChars(string inputString) {
                var result = new List();
                int startIndex = -1;
                for (int i = 0; i < inputString.Length; i++) {
                    var character = inputString[i];
                    if ((character >= 'a' && character <= 'z') ||
                        (character >= 'A' && character <= 'Z')) {
                        if (startIndex == -1) {
                            startIndex = i;
                        }
                        if (i == inputString.Length - 1) {
                            result.Add(GetString(inputString, startIndex, i));
                        }
                        continue;
                    }
                    if (startIndex != -1) {
                        result.Add(GetString(inputString, startIndex, i - 1));
                        startIndex = -1;
                    }
                }
                return result;
            }
    
            public static string GetString(string inputString, int startIndex, int endIndex) {
                string result = "";
                for (int i = startIndex; i <= endIndex; i++) {
                    result += inputString[i];
                }
                return result;
            }
    

提交回复
热议问题