Tag: 字数

计算richtextbox中所有单词的最有效方法是什么?

我正在写一个文本编辑器,需要提供一个实时字数。 现在我正在使用这个扩展方法: public static int WordCount(this string s) { s = s.TrimEnd(); if (String.IsNullOrEmpty(s)) return 0; int count = 0; bool lastWasWordChar = false; foreach (char c in s) { if (Char.IsLetterOrDigit(c) || c == ‘_’ || c == ‘\” || c == ‘-‘) { lastWasWordChar = true; continue; } if (lastWasWordChar) { lastWasWordChar = […]

使用C#计算每个句子中的单词

我需要创建一个程序,显示其中包含最多单词的句子。 string [] st = { “I like apples.”, “I like red apples.”, “I like red apples than green apples.” }; foreach (string s in st) { int NumberOfWords = s.Split(‘ ‘).Length; } 结果应该显示“我喜欢红苹果而不是青苹果”。