How to count the words in a String in C#

Remember the Word Count function in Microsoft Word? Here’s how we could implement counting the words in a String in C#:

The following is the spource code of a program featuring two word counting methods, both of which yield fairly similar results to Microsoft Word from Microsoft Office 2007. The example program below first executes a Regex-based function for counting the words in the string constant, and then executes the loop-based one.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        const string t1 = "To be or not to be, that is the question.";
        Console.WriteLine(WordCounting.CountWords1(t1));
        Console.WriteLine(WordCounting.CountWords2(t1));

        const string t2 = "Mary had a little lamb.";
        Console.WriteLine(WordCounting.CountWords1(t2));
        Console.WriteLine(WordCounting.CountWords2(t2));
    }
}

/// 
/// 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]+");
        return collection.Count;
    }

    /// 
    /// 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 ||                     char.IsPunctuation(s[i]))                 {                     c++;                 }             }         }         if (s.Length > 2)
        {
            c++;
        }
        return c;
    }
}

Program Output

10
10
5
5

It has static methods. This code is ideally contained in static methods as  it doesn’t maintain state or any data. You can think of it as an action, not an object. The methods each receive a string. Both approaches above receive a string and return an integer equal to the number of words they calculate.

The first method uses Regex. The first method here, CountWords1, is better in every way except perhaps performance. It is shorter and simpler to maintain, and is also considerably more accurate. The backslash-S characters (\S) mean characters that are not spaces. So the first method considers each non-letter character to be part of a word, similar to Microsoft Word.

Source: http://dotnetperls.com/word-count

2 thoughts on “How to count the words in a String in C#”

  1. I need a shorter and practical function to count the words delimited by spaces in a string.

    For example: in string like “ab cd de” the function should return 3.

    Any suggestions?

    1. Try the following:


      string s="ab cd de";
      string [] words=s.Split(null);
      int i=words.Length;

      alternatively you could use regex (which would also allow you to change the delimeter later )


      string input = "ab cd de";
      int result = Regex.Matches(input, " ").Count + 1;

Leave a Reply

Your email address will not be published. Required fields are marked *