Using regular expression and Extension Method in C#, code below provides a simple way to count all words to a given string excluding all whitespaces, tabs and line breaks.
1: using System.Text.RegularExpressions;
2:
3: public class MyExtension
4: {
5: /// <summary>
6: /// Count all word occurrence
7: /// </summary>
8: /// <param name="input">string to begin with</param>
9: /// <returns>int</returns>
10: public static int WordCount(this string input)
11: {
12: var count = 0;
13: try
14: {
15: // Exclude whitespaces, Tabs and line breaks
16: var re = new Regex(@"[^\s]+");
17: var matches = re.Matches(input);
18: count = matches.Count;
19: }
20: catch
21: {
22: }
23: return count;
24: }
25: }
and we could use this code like this:
1: string word = "the quick brown\r\nfox jumps over the lazy \tdog.";
2: Console.WriteLine("Total Words: {0}", word.WordCount());
3: // Output: Total Words: 9
The code is also posted in Extension Method website.
Know more about Regular Expressions.
Technorati Tags: C# Extension Method
March 19, 2009 at 9:25 am
what if we want to know how many times a word is repeated?