返回首页

maajanes

回答

评论会员:游客 时间:2012/02/06
你还没有给出任何细节,所以答案只能是一样模糊如果你想指望字符,然后遍历字符串并计算
。。曼弗雷德河Bihy:这里试试这个小样本:


using System;

using System.Collections.Generic;

using System.Windows.Forms;

 

namespace CountCharacters

{

    static class Program

    {

        static void Main()

        {

            Dictionary<Char, int> countChars = new Dictionary<Char,int>();

            String test = "Hello man how's it going. Some characters here in this string. Some stuff to test our character counter with.";

            foreach (Char ch in test)

            {

                if(countChars.ContainsKey(ch))

                {

                    countChars[ch]++;

                }

                else

                {

                    countChars.Add(ch, 1);

                }

            }

            List<CharInt> list = new List<CharInt>();

            foreach (Char key in countChars.Keys)

            {

                list.Add(new CharInt(key, countChars[key]));

            }

            list.Sort();

            foreach (CharInt ci in list)

            {

                Console.WriteLine("{0} : {1}", ci.Char, ci.Int);

            }

            Console.ReadLine();

        }

 

        private class CharInt : IComparable<CharInt>

        {

            public Char Char;

            public int Int;

            public CharInt(Char c, int i)

            {

                this.Char = c;

                this.Int = i;

            }

            public int CompareTo(CharInt other)

            {

                if (other == null) return 1;

                return -1 * this.Int.CompareTo(other.Int);

            }

        }

    }

}

干杯!

mdash;如MRB
评论会员:游客 时间:2012/02/06
CPallini下:一个快速和肮脏的黑客使用Split方法,例如:{C}