What is the most efficient way to count all of the words in a richtextbox?
I am writing a text editor and need to provide a live word count. Right now I am using this extension method: 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 = false; count++; } } if (!lastWasWordChar) count--; return count + 1; } I have it set so that the word count runs on the richtextbox's text every tenth of a second (if the