Word Count Algorithm in C#

前端 未结 7 1600
天涯浪人
天涯浪人 2021-01-05 02:37

I am looking for a good word count class or function. When I copy and paste something from the internet and compare it with my custom word count algorithm and MS Word it is

7条回答
  •  梦毁少年i
    2021-01-05 03:01

    Use a regular expression to find words (e.g. [\w]+) and just count the matches

    public static Regex regex = new Regex(
      "[\\w]+",
    RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );
    

    regex.Match(_someString).Count

提交回复
热议问题