使用c#中的字典计算字符串中每个重复单词的出现次数

时间:2014-04-25 12:11:27

标签: c# string dictionary

编辑:我详细阐述了我的问题..解决方案在这里是一个修复重复的单词..我被问到每个重复的单词

我是新手......可能不是一个好问题。 ......

这是字符串

string str = "this this is is a a string"

在访谈中,我被要求将每个重复关键字的计数存储在通用词典中,然后在订单中显示

例如"的出现是"关键字是2

类似链接
Find the most occurrence of a character in string C#?这是关于寻找字符的

Finding occurrences of words in text which are in a list words这是在python中

Remove occurrences of duplicate words in a string这在javaScript中

How to use the string.match method to find multiple occurrences of the same word in a string?不相关

..请建议

3 个答案:

答案 0 :(得分:11)

使用LINQ非常简单:

string str = "this is is a string";
string[] words = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

(你也可以像@markieo一样使用Regex.Split(str, @"\W+")在他的回答中。区别在于它还会检测被引号和其他标点符号包围的单词。感谢@JonB在评论中指出这方面。 )

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

修改

我发布了满足您增强要求的代码。但是对于未来,只需发布​​另一个问题,而不是修改一个已被回答的问题!

// 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
);

答案 1 :(得分:1)

尝试以上方法:

    string str = "this this is is a a string";
    private int count(string key)
    {
        string[] ar = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
        Dictionary<int, string> d = new Dictionary<int, string>();

        for (int i = 0; i < ar.Length; i++)
            d.Add(i, ar[i]);
       return d.Where(x => x.Value == key).ToList().Count;
    }

函数调用:

count(str, "is");

答案 2 :(得分:0)

您可以使用此代码获取字符串中所有单词的数组。

static string[] SplitWords(string s)
{
     return Regex.Split(s, @"\W+");
}

然后你可以使用foreach循环来计算单词在数组中出现的所有时间。 像这样:

int count = 0;
foreach(string s in SplitWords("This is is a string"){
    if(s == "is"){
    count++;
    }
}

int count是单词在字符串中出现的次数。

来源: http://www.dotnetperls.com/split