wordCount - RegEx count the number of words in a string


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. using System.Text.RegularExpressions;
  2.  
  3. public int WordCount(string txtToCount)
  4. {
  5. string pattern = "\\w+";
  6. Regex regex = new Regex(pattern);
  7.  
  8. int CountedWords = regex.Matches(txtToCount).Count;
  9.  
  10. return CountedWords;
  11. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.