c# - 查找字符串中出现次数最多的单词 C#

标签 c# string algorithm

我正在尝试查找字符串中出现次数最多的单词。

例如

Hello World This is a great world, This World is simply great

根据上面的字符串,我正在尝试计算如下结果:

  • 世界,3
  • 很好,2
  • 你好,1
  • 这个,2

但忽略长度小于 3 个字符的任何单词,例如is发生了两次。

我试图调查 Dictionary<key, value>对,我试图查看 linq 的 GroupBy延期。我知道解决方案介于两者之间,但我就是无法理解算法以及如何完成这项工作。

最佳答案

使用 LINQ 和正则表达式

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())

关于c# - 查找字符串中出现次数最多的单词 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8707208/

相关文章:

c# - 不带组的对象的 Linq 总和

c# - 使用 Linq 的多个外部连接与 2 个连接到同一个表/对象。得到了 SQL,需要 Linq to Entity

python - 在 python 的字典树中搜索值

algorithm - 你如何在算法中定义 "generalized example"?

c# - 更新标签时,程序在For循环上挂起

c# - 正在清除异步问题并等待吗?

在C程序中将2个数组复制到1个数组

MySql : How can i split the string on single "\" backslash

python - 这个字符串标准化器 Python 片段有什么问题?

algorithm - 压缩矩阵乘法