Click here to Skip to main content
12,623,882 members (28,529 online)
Click here to Skip to main content
Add your own
alternative version

Stats

58.3K views
19 bookmarked
Posted

Don't count spaces when counting words.

, 17 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Over the last couple of days I've seen numerous examples of people posting about how to count words in a sentence. Disturbingly, these postings recommend suggest counting the number of spaces in the sentence and use that as the basis of a word count.You may be asking why this is a problem. Well,...
Over the last couple of days I've seen numerous examples of people posting about how to count words in a sentence. Disturbingly, these postings recommend suggest counting the number of spaces in the sentence and use that as the basis of a word count.

You may be asking why this is a problem. Well, consider the following sentence:

The total number of words    \t     in this sentence,is 10.


As you can see, simply counting spaces isn't going to work. There's the special characters (the \t) to take care of, the multiple spaces, and the words separated by a comma without a space. So, if counting spaces doesn't work, what does? The answer is to use a regular expression, and you are going to love how simple it is. There's a simple regular expression that matches words, and takes care of all the guff demonstrated above; all you need to match a word is use \w+. Here's a quick sample:

Regex regex = new Regex("\\w+",  RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant);
string input = "The total number of words       \t        this sentence is 10.";
MatchCollection match = regex.Matches(input);

License

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

Share

About the Author

Pete O'Hanlon
CEO
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.

I am not the Stig, but I do wish I had Lotus Tuned Suspension.

You may also be interested in...

Pro

Comments and Discussions

 
QuestionDefinition of word? Pin
Andreas Gieriet12-Mar-12 23:42
memberAndreas Gieriet12-Mar-12 23:42 
AnswerRe: Definition of word? Pin
pwasser19-Mar-12 16:23
memberpwasser19-Mar-12 16:23 
GeneralRe: Definition of word? Pin
Andreas Gieriet19-Mar-12 21:38
memberAndreas Gieriet19-Mar-12 21:38 
GeneralRe: Definition of word? Pin
pwasser19-Mar-12 21:50
memberpwasser19-Mar-12 21:50 
GeneralRe: Definition of word? Pin
Andreas Gieriet19-Mar-12 22:31
memberAndreas Gieriet19-Mar-12 22:31 
GeneralRe: Definition of word? Pin
pwasser19-Mar-12 23:50
memberpwasser19-Mar-12 23:50 
GeneralRe: Definition of word? Pin
Andreas Gieriet20-Mar-12 1:35
memberAndreas Gieriet20-Mar-12 1:35 
GeneralRe: Definition of word? Pin
Andreas Gieriet20-Mar-12 5:32
memberAndreas Gieriet20-Mar-12 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 18 Oct 2011
Article Copyright 2010 by Pete O'Hanlon
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid