Get first 250 words of a string?

前端 未结 6 537
梦如初夏
梦如初夏 2020-12-11 01:51

How do I get the first 250 words of a string?

6条回答
  •  [愿得一人]
    2020-12-11 02:46

    It's possible without calling Take().

    string[] separatedWords = stringToProcess.Split(new char[] {' '}, 250, StringSplitOptions.RemoveEmptyEntries);
    

    Which also allows splitting based on more than just space " " and therefore fixes occurrences when spaces are incorrectly missing in input.

    string[] separatedWords = stringToProcess.Split(new char[] {' ', '.', ',' ':', ';'}, 250, StringSplitOptions.RemoveEmptyEntries);
    

    Use StringSplitOptions.None, if you want empty strings to be returned instead.

提交回复
热议问题