2013-04-18 73 views
4

如果我有一串长长的文本,并且想要拉出长度大于4个字符并且在字符串中找到4次以上的单词,那么LINQ可以这样做吗?LINQ可以用来从字符串中提取关键字吗?

+2

http://stackoverflow.com/questions/9879060/need-a-linq-query-to-find-string-items回答似乎它会为你做的伎俩。 – 2013-04-18 03:49:11

回答

15

您可以收紧这件事,但我相信这将是东西向的

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

你会的,当然需要的效果来决定你希望如何处理任何标点符号可能出席。

所以给出的输入

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"; 

结果包括“快速”和“跳楼”,因为唯一的其他字大于4个字符(“棕色”)只出现3次。

+0

完美,谢谢! – Chaddeus 2013-04-18 04:04:10