Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / regular-expression

Don't count spaces when counting words.

5.00/5 (7 votes)
18 Oct 2011CPOL 10K  
The Regex method uses comma, full stop, hyphen, and apostrophe as word separators. The problem with this is that these characters are not universally used as separators. The hyphen is used to join two words. Could I suggest the following?string input = Mr O'Brien-Smith arrived at 8.30 and...

The Regex method uses comma, full stop, hyphen, and apostrophe as word separators. The problem with this is that these characters are not universally used as separators. The hyphen is used to join two words. Could I suggest the following?


C#
string input = "Mr O'Brien-Smith arrived at 8.30 and spent \t $1,000.99";
string[] words= input.Split(default(Char[]), StringSplitOptions.RemoveEmptyEntries);

This gives 8 as the number of words. Regex returns 13 matches.

License

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