有个字符串 str=a,b,c,a,e,f,d,a,e,a
现在要统计出 这个字符串中 有多少个a,多少个b,多少个c

解决方案 »

  1.   

    不会做,但有点思路,不知道对不对
    做个循环把字符串中各个数字取一遍substring(i,i+1)
    然后计数
    case a  anum+=1
    case b  bnum+=1
    ...代码不大会写,请指教.
      

  2.   


    /// <summary>
        /// 个数
        /// </summary>
        /// <param name="strText">字符串</param>
        /// <param name="strWord">单词:a,b,c,d,</param>
        /// <returns></returns>
    public static int CountWord(string strText, string strWord)
        {
            int count = 0;
            if (!string.IsNullOrEmpty(strText))
            {
                string[] strWords = strText.Split(',');            for (int i = 0; i < strWords.Length; i++)
                {
                    if (strWords [i] == strWord)         //是否相等
                    {
                        count++;
                    }
                }            
            }
            return count;     
        }
      

  3.   

            private string GetSameCount(string str)
            {
                int length=str.Length;
                string result=string.Empty;
                if(length==0) return result;
                while(length>0)
                {
                    string tempChar=str[0].ToString();
                    string tempStr=str.Replace(tempChar,"");
                    int tempLen=tempStr.Length;
                    result+=(length-tempLen)+tempChar;
                    length=tempLen;
                }
                return result;
            }调用时:
    str="a,b,c,a,e,f,d,a,e,a";
    string result=GetSameCount(str.Replace(",",""));
      

  4.   

                string s="asdfasdf";
                Dictionary<char, int> list = new Dictionary<char, int>();
                foreach (char c in s.ToCharArray())
                {
                    if (list.ContainsKey(c))
                    {
                        list[c] += 1;
                    }
                    else
                    {
                        list.Add(c, 1);
                    }
                }
      

  5.   

    完整代码:
                string s="asdfasfdasedfdadfddddddf";
                Dictionary<char, int> list = new Dictionary<char, int>();
                foreach (char c in s.ToCharArray())
                {
                    if (list.ContainsKey(c))
                    {
                        list[c] += 1;
                    }
                    else
                    {
                        list.Add(c, 1);
                    }
                }
                foreach (KeyValuePair<char, int> item in list)
                {
                    Console.WriteLine("字符{0}有{1}", item.Key, item.Value);
                }
                Console.Read();
      

  6.   

    建立一个数组count[26],记录a-z的计数int[] count = new int[26];
    for(int k = 0; k < str.Length; k++)
    {
       if(char.IsLetter(str[k]))
       {
          int n = str[k] - 'A';
          if(n > 26) 
          {
             n = str[k] - 'a';
          }
          count[n]++;
        }
    }