C# Word CountThis C# example program shows how to count words. It has results similar to Microsoft Word.
Word count. Strings often contain multiple words. These words can be counted in a C# method. The total count must be similar to Microsoft Word 2007. Words are broken by punctuation, a space, or by being at the start or the end of string.
First, here we see two word counting methods, both of which yield fairly similar results to Microsoft Word from Microsoft Office 2007. The example program first executes the Regex word count function, and then the loop-based one.Regex.Matches
Static: This code is ideally contained in static methods because it doesn't maintain state or any data. You can think of it as an action, not an object.Static
CountWords1: This is shorter and simpler to maintain, and is also more accurate. The backslash-S characters (\S) mean characters that are not spaces.
So: CountWords1 considers each non-letter character to be part of a word, similar to Microsoft Word.
CountWords2: This version of the code uses a for-loop, and tries to correctly count word breaking characters.ForChar
C# program that counts words
static void Main()
const string t1 = "To be or not to be, that is the question.";
const string t2 = "Mary had a little lamb.";
/// Contains methods for counting words.
public static class WordCounting
/// Count words with Regex.
public static int CountWords1(string s)
MatchCollection collection = Regex.Matches(s, @"[\S]+");
/// Count word with loop and character tests.
public static int CountWords2(string s)
int c = 0;
for (int i = 1; i < s.Length; i++)
if (char.IsWhiteSpace(s[i - 1]) == true)
if (char.IsLetterOrDigit(s[i]) == true ||
if (s.Length > 2)
Accuracy. Microsoft Office dominates the business world, so I will provide some stats about the results of these two algorithms versus Microsoft Word 2007. The Regex method, has results that differ by about 0.02% from Microsoft Word.
Accuracy of word counting methods:
Microsoft Word: 4007 words
Regex method: 3990 words [closest]
Loop method: 3973 words
Microsoft Word: 1414 words
Regex method: 1414 words [closest]
Loop method: 1399 words
Microsoft Word: 462 words
Regex method: 463 words [closest]
Loop method: 459 words
Microsoft Word: 470 words
Regex method: 470 words [closest]
Loop method: 465 words
Microsoft Word: 2742 words
Regex method: 2738 words [closest]
Loop method: 2710 words
Example input and output
Input: To be or not to be, that is the question.
Mary had a little lamb.
Word count: 10
The second method, which tests each character in a loop, would be many times faster if carefully benchmarked. It is nearly optimal, while the Regex-based method would draw in far more computation. Regular expressions are relatively slow.
However: Their greater ease of use and clarity is often more important. In scripting languages, regular expressions often perform better.
Tip: You can store the Regex object it uses as an instance member or field of the class.
Then: You can simply call its instance Matches method instead of the static Regex.Matches method. This improves speed.
What should you do if you need to specify that a certain character, such as the pound sign (#), is also a word separator? In this addition to the article, we use character ranges to specify valid word characters.
Note: If you omit a character from the ranges, that character is considered a word separator.
Here: You can see that with this version of the Regex, the substring "is#the#question" is treated as three separate words.
Tip: This is because the pound sign is not included in the ranges of valid characters in the pattern.
And: With this form of the Regex pattern, you can more easily change which characters are valid and which are not.
C# program that uses modified Regex
static void Main()
const string t1 = "To be or not to be, that is#the#question.";
static int CountWordsModified(string s)
return Regex.Matches(s, @"[A-Za-z0-9]+").Count;
Summary. We saw two word count methods, both of which provide results similar to Microsoft Word 2007. The first method, the Regex-using one, is considerably closer to Microsoft Word's results. However, there is a small percentage difference.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.