簡體   English   中英

字符串數組中的字符串計數頻率

[英]Count frequency of string in string array

我有用戶名列表(字符串)。 每個名稱cn [i]都會具有幾個技能:

cn[0] has: "text1, text2, text3"
cn[1] has: "text2, text4, text6"
cn[2] has: "text6, text8"
cn[2] has: "text11, text8, text1, text4, text2"

等等

現在,我需要計算總共有多少技能,每項技能有多少人。 所以我認為它將包含以下步驟:

1. add text1, text2, ... to an array (I don't know how to get each string and get rid of the comma "'")
2. suppose we have 

    string[] stringArray = { "text1", "text2", "text3", "text4", "text6", ... };

我們可以通過以下方式檢查頻率:

foreach (string x in stringArray)
{
    if (x.Contains(stringToCheck))
    {
        // increase the number of count
    }
}

3.我不知道如何將計數數字粘貼到每個技能上,然后我們可以顯示它。 我在想像Map map = new HashMap();

您可以使用System.LinqGroupByToDictionary擴展方法來執行任務:

using System.Linq;

var frequency = cn
    .SelectMany(u => u.Split(new string[] { ", " }, StringSplitOptions.None))
    .GroupBy(s => s)
    .ToDictionary(g => g.Key, g => g.Count());

var numberOfSkills = frequency.Count;

var numberOfUsersWithSkillOne = frequency["SkillOne"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM