Word Count Algorithm in C#

前端 未结 7 1466
天涯浪人
天涯浪人 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:24

    public static class WordCount
    {
        public static int Count(string text)
        {
            int wordCount = 0;
            text = text.Trim();// trim white spaces
    
            if (text == ""){return 0;} // end if empty text
    
            foreach (string word in text.Split(' ')) // or use any other char(instead of empty space ' ') that you consider a word splitter 
            wordCount++;
            return wordCount;
        }
    }
    

提交回复
热议问题