c#统计字符个数LINQ

c#统计字符个数LINQ

复制代码
 private void CountCharTest()
        {
            string strTest = "11111abcaabb!@??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,3}", groupText.Count());

            }
            foreach(var groupText in query)
            {
                if((char)groupText.Key=='1')
                {
                    Console.WriteLine("1的数量:-------------------{0,3}", groupText.Count());
                }
            }
            /*
             * ----------------------------abcdefg
“WaferSawRouteInspection.exe”(CLR v4.0.30319: WaferSawRouteInspection.exe): 已加载“D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Remote Debugger\x64\Runtime\Microsoft.VisualStudio.Debugger.Runtime.Desktop.dll”。已跳过加载符号。模块进行了优化,并且调试器选项“仅我的代码”已启用。
-------------------  ! -------------------  ? -------------------  @ -------------------  a -------------------  b -------------------  c -------------------  d 
-------------------  1-------------------  2-------------------  1-------------------  3-------------------  3-------------------  1-------------------  1

             * */

            Console.WriteLine();
            Console.WriteLine("中文显示-------------------abcdefg");
        }
复制代码

 

posted @   txwtech  阅读(167)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2021-11-15 sphelper.h编译错误解决方法
2019-11-15 cc4a-c++类定义与struct定义方式代码示范
点击右上角即可分享
微信分享提示