Click here to Skip to main content
15,867,771 members
Articles / General Programming / Regular Expressions
Alternative
Tip/Trick

Don't count spaces when counting words.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
18 Oct 2011CPOL 9.1K   2   2
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)


Written By
Student
Wales Wales
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 nice Pin
beginner201124-Oct-11 16:43
beginner201124-Oct-11 16:43 
GeneralOr you could just change the Regex pattern to: "[^\\s]+" Pin
Richard Deeming24-Oct-11 8:09
mveRichard Deeming24-Oct-11 8:09 

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.