Click here to Skip to main content
Click here to Skip to main content
Alternative Tip
Go to top

Don't count spaces when counting words.

, 25 Oct 2011
Rate this:
Please Sign up or sign in to vote.
I also use a Regex expression to count words, which returns the same number of words as MS Word. I wrap the Regular Expression in a String extension method to make it easy to use.public static class StringExtensions{ /// /// WordCounts Regular Expression /// ...

I also use a Regex expression to count words, which returns the same number of words as MS Word. I wrap the Regular Expression in a String extension method to make it easy to use.

public static class StringExtensions
{
  /// <summary>
  /// WordCounts Regular Expression
  /// </summary>
  private const string WordCountRegex = @"[^\s!?¡¿\-\–]+";
 
  /// <summary>
  /// Static WordCounts Regular Expression Object
  /// </summary>
  private static Regex regexWordCounts = new Regex(WordCountRegex, 
             RegexOptions.Compiled | RegexOptions.Multiline);
  
  /// <summary>
  /// Returns the number of words in a given <paramref name="sentence" />
  /// </summary>
  /// <param name="sentence">Text in which to count words</param>
  /// <returns>Number of words, or zero if regular expression failed</returns>
  public static int WordCounts(this string sentence)
  {
    try
    {
      MatchCollection matchCollection = regexWordCounts.Matches(sentence);
      return matchCollection.Count;
    }
    catch
    {
      return 0;
    }
  }
}

Taking the samples above, this would give the following:

string input = 
  "The total number of words       \t        this sentence is 10.";
int wordCounts = input.WordCounts(); //Returns 9

input = "Mr O'Brien-Smith arrived at 8.30 and spent \t $1,000.99";
int wordCounts = input.WordCounts(); //Returns 9

Hope this helps.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jacobs76
Software Developer (Senior) Lionbridge
France France
No Biography provided

Comments and Discussions

 
GeneralShould "Mr O'Brien-Smith arrived at 8.30 and spent \t $1,000... PinmemberFDW6-Dec-11 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 25 Oct 2011
Article Copyright 2011 by Jacobs76
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid