求算一字符串中独立字符的个数。
如string str="jsffff";
其中 
j,1
s,1
f,4

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    namespace 字符统计
    {
        class Program
        {
            static void Main(string[] args)
            {
                string myString =  "uhifhiohskljhdsafipo喔喔 哈哈 你好啊";
                Dictionary<char, int> myDictionary = MyMethod.GetInfoFormString(myString);
                foreach(KeyValuePair<char,int> kvp in myDictionary)
                {
                    Console.WriteLine("Key = {0}, Value = {1}",
                        kvp.Key, kvp.Value);
                }           
                
            }    }
        public static class MyMethod
        {
            public static Queue<char> GetOnlyCharFromString(string str)
            {
                Queue<char> myQueue = new Queue<char>();
                for (int i = 0; i < str.Length; i++)
                {
                    if (myQueue.Contains(str[i]) == false)
                        myQueue.Enqueue(str[i]);
                }
                return myQueue;
            }
            public static Dictionary<char,int> GetInfoFormString(string str)
            {
                Queue<char> myQueue = GetOnlyCharFromString(str );
                Dictionary<char,int> myDictionary=new Dictionary<char,int>();
                int oldQueueCount = myQueue.Count;//记录下它原有的量;
                for(int i=0;i<oldQueueCount ;i++)
                {
                    char myChar=myQueue.Dequeue();
                    int num=0;
                    for(int j=0;j<str.Length;j++)
                    {
                        if(str[j]==myChar)
                            num++;
                    }
                    myDictionary.Add(myChar,num);
                }
                return myDictionary;
            }    }}