This utility helps you to calculate number of words in a given string. This method usage the regular expression to calculate word count.
/// <summary>
/// Returns word count.
/// </summary>
public static int WordCount(string strToCount)
{
int words;
Regex reg = new Regex(@"\w+");
MatchCollection mc = reg.Matches(strToCount);
if (mc.Count > 0)
words = mc.Count;
else
words = 0;
return words;
}
if i had a string with the text in, how would I incorporate it into this script?
ReplyDeletee.g.
string Text = "I Want to count this.";
Hi Dan,
ReplyDeleteJust call this method and pass input string just by using following script
int ResultCount = WordCount(Text);
After executing this line you will have 5 in ResultCount for the given string.