65.9K
CodeProject is changing. Read more.
Home

Don't count spaces when counting words.

starIconstarIconstarIconstarIconstarIcon

5.00/5 (7 votes)

Oct 18, 2011

CPOL
viewsIcon

9951

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?

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.