Word Count Algorithm in C#

前端 未结 7 1586
天涯浪人
天涯浪人 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 03:06

    As @astander suggests, you can do a String.Split as follows:

    string[] a = s.Split(
        new char[] { ' ', ',', ';', '.', '!', '"', '(', ')', '?' },
        StringSplitOptions.RemoveEmptyEntries);
    

    By passing in an array of chars, you can split on multiple word breaks. Removing empty entries will keep you from counting non-word words.

提交回复
热议问题