Click here to Skip to main content
11,414,561 members (63,959 online)
Click here to Skip to main content
Alternative Tip/Trick

Don't count spaces when counting words.

, 18 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

George Swan
Student
Wales Wales
No Biography provided

Comments and Discussions

 
GeneralOr you could just change the Regex pattern to: "[^\\s]+" Pin
Richard Deeming24-Oct-11 9:09
memberRichard Deeming24-Oct-11 9:09 
Or you could just change the Regex pattern to: "[^\\s]+"

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 | Terms of Use | Mobile
Web02 | 2.8.150427.4 | Last Updated 18 Oct 2011
Article Copyright 2011 by George Swan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid