Wern Ancheta Wern Ancheta - 4 months ago 31
C# Question

Counting number of words in C#

I'm trying to count the number of words from a rich textbox in C# the code that I have below only works if it is a single line. How do I do this without relying on regex or any other special functions.

string whole_text = richTextBox1.Text;
string trimmed_text = whole_text.Trim();
string[] split_text = trimmed_text.Split(' ');
int space_count = 0;
string new_text = "";

foreach(string av in split_text)
if (av == "")
new_text = new_text + av + ",";

new_text = new_text.TrimEnd(',');
split_text = new_text.Split(',');
MessageBox.Show(split_text.Length.ToString ());


Since you are only interested in word count, and you don't care about individual words, String.Split could be avoided. String.Split is handy, but it unnecessarily generates a (potentially) large number of String objects, which in turn creates an unnecessary burden on the garbage collector. For each word in your text, a new String object needs to be instantiated, and then soon collected since you are not using it.

For a homework assignment, this may not matter, but if your text box contents change often and you do this calculation inside an event handler, it may be wiser to simply iterate through characters manually. If you really want to use String.Split, then go for a simpler version like Yonix recommended.

Otherwise, use an algorithm similar to this:

var text = richTextBox1.Text.Trim();
int wordCount = 0, index = 0;

while (index < text.Length)
   // check if current char is part of a word
   while (index < text.Length && !char.IsWhiteSpace(text[index]))


   // skip whitespace until next word
   while (index < text.Length && char.IsWhiteSpace(text[index]))

This code should work better with cases where you have multiple spaces between each word.