例如在 "aaa efaf feaf faefawef vsgrgs aaa efaf afergrgrg aaa"中那个字符串出现次数最多,出现多少次

解决方案 »

  1.   

    字符串的匹配最好有规则
    比如:aaa中,aa字符串,出现次数怎么算,算1次还是算2次???
    然后再确定怎么来算出现的次数
      

  2.   


                string str = "aaa efaf feaf faefawef vsgrgs aaa efaf afergrgrg aaa";
                Dictionary<string, int> dic = new Dictionary<string, int>();
                string[] temp = str.Split(' ');
                foreach (string s in temp)
                {
                    if (!dic.ContainsKey(s))
                        dic.Add(s, 1);
                    else
                        dic[s]++;
                }
                string most = string.Empty;
                int count = 0;
                foreach (string s in dic.Keys)
                {
                    if (dic[s] > count)
                    {
                        most = s;
                        count = dic[s];
                    }
                }
                Console.WriteLine("出现最多的是:{0},共出现了{1}次", most, count);
      

  3.   

                string str = "aaa efaf feaf faefawef vsgrgs aaa efaf afergrgrg aaa";            string[] strs = str.Split(' ');            System.Collections.Hashtable table = new System.Collections.Hashtable();
                foreach (string tmpStr in strs)
                {
                    if (table[tmpStr] == null)
                    {
                        table[tmpStr] = 1;
                    }
                    else
                    {
                        table[tmpStr] = (int)table[tmpStr] + 1;
                    }
                }
                int maxCount = 0;
                foreach (System.Collections.DictionaryEntry de in table)
                {
                    if ((int)de.Value > maxCount)
                    {
                        maxCount = (int)de.Value;
                    }
                }
                System.Collections.ArrayList maxStr = new System.Collections.ArrayList();            foreach (System.Collections.DictionaryEntry de in table)
                {
                    if ((int)de.Value == maxCount)
                    {
                        maxStr.Add(de.Key);
                    }
                }
                foreach (object obj in maxStr)
                {
                    Console.Out.WriteLine((string)obj);
                }
      

  4.   

    maxCount是出现的次数,如果只有一个字串会出现最多次,maxStr就会只有一个值,