使用字典在C#计数每重复单词的出现在一个字符串 [英] Counting the occurrences of every duplicate words in a string using dictionary in c#
问题描述
编辑:我阐述我的问题更多..solutions在这里为一个固定重复的一句话。我问到的每一句话重复
Edit :I elaborated My Question More ..solutions are here for one fix duplicate word .. I was asked about every duplicate word
我是新手.. .Might是不是一个好问题。 ......
I am a Newbie ...Might be not a Good question . ......
这是字符串
string str = "this this is is a a string"
在接受采访时,有人问我存储在一般的字典,然后显示它们每一个重复关键字的顺序计数
in Interview ,i was asked to store the count of every duplicate keyword in generic Dictionary and then display them in Order
例如否次数的是关键字是2
for example No of occurance of "is" keyword is 2
相似的链接
:
的查找字符串C#字符的最发生?这就是找到人物
similar Links
:
Find the most occurrence of a character in string C#? this is about finding character
Finding occurrences of words in text which are in a list words this is in python
的以JavaScript
字符串这删除重复出现的话
的如何使用string.match方法查找字符串中的同一个词多次出现?不相关
..请推荐
推荐答案
与LINQ很简单:
string str = "this is is a string";
string[] words = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
(你可以选择使用 Regex.Split(STR,@\ W +)
像@markieo的确在他的回答。不同的是,它还将检测由报价和其他标点符号。感谢@JonB包围字评论中指出了这方面。)
(You could alternatively use Regex.Split(str, @"\W+")
like @markieo did in his answer. The difference is that it will also detect words surrounded by quotes and other punctuation marks. Thanks for @JonB for pointing this aspect out in comments.)
Dictionary<string, int> statistics = words
.GroupBy(word => word)
.ToDictionary(
kvp => kvp.Key, // the word itself is the key
kvp => kvp.Count()); // number of occurences is the value
int isCount = statistics["is"]; // returns 2
编辑:
我张贴代码解决您的增强要求。但对于未来,只是后一个问题,而不是修改一个一个已经回答了!
I'm posting code addressing your enhanced requirements. But for the future, just post another question instead of modifying one that's been answered!
// retrieving all duplicate words
string[] duplicates = statistics
.Where(kvp => kvp.Value > 1)
.Select(kvp => kvp.Key)
.ToArray();
// counting all duplicates and formatting it into a list in the desired output format
string output = String.Join(
"\n",
statistics
.Where(kvp => kvp.Value > 1)
.Select(kvp =>
String.Format(
"count(\"{0}\") = {1}",
kvp.Key,
kvp.Value))
.ToArray() // this line is only needed on older versions of .NET framework
);
这篇关于使用字典在C#计数每重复单词的出现在一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!