LINQ Getting the longest word in text content

By Indranil Chatterjee

This explains a way to search for the longest word in a text/paragraph using LINQ.

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 longestWord = words.OrderByDescending(w => w.Length).First();
Console.WriteLine("The longest word is '{0}'", longestWord);
//This should print 'processing' to the Console output

LINQ Getting the longest word in text content  (467 Views)