(LINQ)统计一个字符串中各个字符的个数

由于最近在学LINQ,在学习过程中突然想起了当前看C的时候的一道题目"统计一个字符串中各个字符的个数"。

发现用LINQ实在是太方便了。。

CODE:

class CountChar
    {
        static void Main(string[] args)
        {
            string strTest = "abcaabb!@??d";

            var query =
                from ch in strTest
                group ch by (int)ch into gr                            //分组条件我是以ASCII大小排序,因为想不出其它方法
                orderby gr.Key
                select gr;
         
            foreach ( var groupText in query )
                Console.Write("{0,3} ", (char)groupText.Key);

            Console.WriteLine();
            foreach (var groupText in query)
            {
                 Console.Write("{0,4}",groupText.Count());
              
            }

            Console.WriteLine();

   }

在VS 2008 Console + WIN 7 调试成功。

posted @ 2009-08-23 19:21  Mangos  阅读(518)  评论(0)    收藏  举报