Chaddeus
Chaddeus

Reputation: 13356

Can LINQ be used to pull keywords out of a string?

If I have a long-ish string of text and wanted to pull out words which are greater than 4 characters in length and found more than 4 times in the string, can LINQ do that?

Upvotes: 4

Views: 88

Answers (1)

Anthony Pegram
Anthony Pegram

Reputation: 126814

You may be able to tighten this up, but I believe it will be something to the effect of

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 wish to deal with any punctuation that might 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 "quick" and "jumped" because the only other word greater than 4 characters ("brown") appeared just 3 times.

Upvotes: 15

Related Questions