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

it2022-05-09  18

由于最近在学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 调试成功。

转载于:https://www.cnblogs.com/loveasm/archive/2009/08/23/1552479.html


最新回复(0)