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"); }
欢迎讨论,相互学习。
cdtxw@foxmail.com