Can LINQ be used to pull keywords from a string?

If I have a long line of text and want to pull out words longer than 4 characters and find more than four times in a line, can LINQ do this?

+4
source share
1 answer

You may be able to tighten it, but I think it will be something like an effect

var results = inputstring.Split() .Where(word => word.Length > 4) .GroupBy(word => word) .Where(grp => grp.Count() > 4) .Select(grp => grp.Key); 

You will of course need to decide how you want to deal with any punctuation that may be present.

So, given the input

 var inputstring = @"The quick brown fox jumped over the lazy dog The quick brown fox jumped over the lazy dog The quick fox jumped over the lazy dog The quick fox jumped over the lazy dog The quick brown fox jumped over the lazy dog"; 

The results contain "fast" and "jumping" because the only other word with more than 4 characters ("brown") appeared only 3 times.

+15
source

All Articles