且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编程以查找字符串中字符的出现次数

更新时间:2022-11-07 22:37:20

看这里:

Look here:
var chars = "NAMAN";
chars.Distinct().ToList().ForEach(c => Console.WriteLine("{0}={1}", c, chars.Count(i => i == c)));





[更新:不需要ToCharArray()]


我使用解决方案3中的LINQ进行了显着改进:



I have a significant improvement using LINQ from solution 3:

str.ToCharArray().GroupBy(i => i).OrderBy(k => k.Key).ToList().
  ForEach(m => Console.WriteLine(string.Format("{0}: {1}", m.Key, m.Count())));


嗅,嗅......是的,闻起来像家庭作业!



对不起,但你得到的答案可能会让你在课堂上成绩不及格。根据你获得的任务水平,你的课程没有办法覆盖你在这里得到的答案所展示的方法,教授课程的人知道你没有写代码。
Sniff, sniff... Yep, smells like homework!

Sorry, but the answers you got will probably get you a failing grade in your class. With the level of the assignment you got, there''s no way your class covered the methods demonstrated in the answers you got here and the person teaching the class will KNOW you didn''t write the code.