Function to Count Words in C# (ASP.NET)
Here’s a C# function to count the number of words in a string:
public static int WordCount (string Text) { string tmpStr;tmpStr = Text.Replace("\t", " ").Trim(); tmpStr = tmpStr.Replace("\n", " "); tmpStr = tmpStr.Replace("\r", " ");while (tmpStr.IndexOf(" ") != -1) tmpStr = tmpStr.Replace(" ", " ");return tmpStr.Split(' ').Length; }


Thanks dude.. your code is really good.. solved my problem in a jiffy..
Very useful. Thanks.
Thanks!!
try this:
//using System.Text.RegularExpressions;
public static int WordCount(string txt)
{
int w;
Regex reg = new Regex(”\w+”);
MatchCollection mc = reg.Matches(txt);
if (mc.Count > 0)
w = mc.Count;
else
w = 0;
return w;
}
Thanks buddy,
Simple and fast!
Codes like this make the work so smooth!
I think the Regex reg = new Regex(”\w+”); is the easiest way here
thanks dude………….. you saved my lots of time….