String str = "this krishna hi krishna this this ravi ramesh this suresh this"; string searchTerm = "this"; // Count the matches, using Regex int wordCount = Regex.Matches( str, searchTerm ).Count;
using System.Text.RegularExpressions;
Thanks, A2H My Blog
//Your string String str = "this krishna hi krishna this this ravi ramesh this suresh this"; string searchTerm = "this"; //Convert the string into an array of words string[] source = str.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries); // Create the query. Use ToLowerInvariant to match "data" and "Data" var matchQuery = from word in source where word.ToLowerInvariant() == searchTerm.ToLowerInvariant() select word; // Count the matches, which executes the query. int wordCount = matchQuery.Count();
using System.Linq;
Login to post response