Using LINQ to find the number of times a given word occurs in a text/paragraph

By Indranil Chatterjee

This explains a way to find out the number of times a given word occurs in a text/paragraph.

var text = "This is a paragraph which needs to be analyzed for word processing. " +
"The term processing includes searching for most repeated words, the longest word in this " +
"text phrase and to find the number of times a given word is repeated in this text.";
var words = text.Split(
new char[] { ' ', '\u000A', ',', '.', ';', ':', '-', '_', '/' },
StringSplitOptions.RemoveEmptyEntries);
var findWord = words.Where(w => w.ToUpper().Contains("processing".ToUpper()));

Console.WriteLine(@"The word '{0}' occurs {1} times.", "processing", findWord.Count());
//This should print out the text: The word 'processing' occurs 2 times.

Using LINQ to find the number of times a given word occurs in a text/paragraph  (756 Views)