如何计算字符串中每个单词的出现次数?

编程入门 行业动态 更新时间:2024-10-17 21:15:25
本文介绍了如何计算字符串中每个单词的出现次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用以下代码从字符串输入中提取单词,如何获得每个单词的出现次数呢?

I use the following code to extract words from string input, how can I get the occurrence of each words too?

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

推荐答案

您可以使用 string.Split 代替 Regex.Split 来获取每个单词的计数,例如:

Instead of Regex.Split you can use string.Split and get the count for each word like:

string str = "Some string with Some string repeated"; var result = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries) .GroupBy(r => r) .Select(grp => new { Word = grp.Key, Count = grp.Count() });

如果您想过滤出那些至少重复10次的单词,则可以在 Select 之前添加一个条件,例如 Where(grp => grp.Count> = 10)

If you want to filter out those words which are repeated 10 times atleast then you can add a condition before Select like Where(grp=> grp.Count >= 10)

对于输出:

foreach (var item in result) { Console.WriteLine("Word: {0}, Count:{1}", item.Word, item.Count); }

输出:

Word: Some, Count:2 Word: string, Count:2 Word: with, Count:1 Word: repeated, Count:1

对于不区分大小写的分组,您可以将当前的GroupBy替换为:

For case insensitive grouping you can replace the current GroupBy with:

.GroupBy(r => r, StringComparer.InvariantCultureIgnoreCase)

因此您的查询将是:

var result = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries) .GroupBy(r => r, StringComparer.InvariantCultureIgnoreCase) .Where(grp => grp.Count() >= 10) .Select(grp => new { Word = grp.Key, Count = grp.Count() });

更多推荐

如何计算字符串中每个单词的出现次数?

本文发布于:2023-11-30 17:32:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   单词   次数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!