2013-04-18 75 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