2011-09-06 33 views

回答

10
string text = "yay blah blah blah blah woo woo yay yay yay."; 

var words = Regex.Split(text, @"\W+") 
    .AsEnumerable() 
    .GroupBy(w => w) 
    .Where(g => g.Count() > 3) 
    .Select(g => g.Key); 

words.ToList().ForEach(Console.WriteLine); 

輸出:


等等

0

雖然沒有肯定最好的(閱讀最efficinent)的方式來解決這個問題,你可以先用空格,逗號,分號等來分割文本。然後,在結果列表中的每個單詞你嘗試將其添加到Dictionary<string,int>

if (!dictionary.TryGetValue(word, out int count)) 
{ 
    dictionary.Add(word, 1); 
} 
else 
{ 
    dictionary[word] += 1; 
} 

然後,你遍歷字典,讓每一個鍵具有三個或更多的計數。

+0

而Linq是在哪裏? –

+0

在編輯問題之前,沒有Linq的要求。 – User

+0

ASP.NET在哪裏? – recursive

相關問題