Thr*_*r3e 8 c# string algorithm
我试图在字符串中找到最常见的单词.
例如
Hello World This is a great world, This World is simply great
Run Code Online (Sandbox Code Playgroud)
从上面的字符串我试图计算结果如下:
但忽略长度小于3个字符的任何单词,例如is两次出现的单词.
我试图调查Dictionary<key, value>对,我试图调查linq的GroupBy扩展.我知道解决方案介于两者之间,但我无法理解算法以及如何完成这项工作.
Ili*_*a G 18
使用LINQ和Regex
Regex.Split("Hello World This is a great world, This World is simply great".ToLower(), @"\W+")
.Where(s => s.Length > 3)
.GroupBy(s => s)
.OrderByDescending(g => g.Count())
Run Code Online (Sandbox Code Playgroud)
因此,我会避免使用LINQ和Regex等,因为它听起来像是在试图找到一个算法,并且理解这不会使用某些函数来为您完成.
并非那些不是有效的解决方案.他们是.当然.
尝试这样的事情
Dictionary<string, int> dictionary = new Dictionary<string, int>();
string sInput = "Hello World, This is a great World. I love this great World";
sInput = sInput.Replace(",", ""); //Just cleaning up a bit
sInput = sInput.Replace(".", ""); //Just cleaning up a bit
string[] arr = sInput.Split(' '); //Create an array of words
foreach (string word in arr) //let's loop over the words
{
if (word.Length >= 3) //if it meets our criteria of at least 3 letters
{
if (dictionary.ContainsKey(word)) //if it's in the dictionary
dictionary[word] = dictionary[word] + 1; //Increment the count
else
dictionary[word] = 1; //put it in the dictionary with a count 1
}
}
foreach (KeyValuePair<string, int> pair in dictionary) //loop through the dictionary
Response.Write(string.Format("Key: {0}, Pair: {1}<br />",pair.Key,pair.Value));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14041 次 |
| 最近记录: |