• C# Dictionary 用法 -- 查找字符串中同一个字符出现次数


     string str ="welcome to bejing , hello china,jiyou wuhan";
     Dictionary<char, int> dict = new Dictionary<char, int>();
    
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i] == ' ')
                    {
                        continue;
                    }
    
                    if (dict.ContainsKey(str[i]))
                    {
                        dict[str[i]]++;
                    }
                    else
                    {
                        dict[str[i]] = 1;
                    }
    
                }
    
                foreach (KeyValuePair<char,int> k in dict)
                {
                    Console.WriteLine("字符:{0} 共出现{1}次",k.Key,k.Value);
    
                }
    
                Console.ReadKey();

    运行结果:

  • 相关阅读:
    SpringMVC截图版
    MySQL
    Mybatis-Spring
    Spring
    get
    log
    SpringBoot
    Mybatis和spingboot整合
    学习笔记-英语
    PartialView+$.reload()局部刷新
  • 原文地址:https://www.cnblogs.com/nymz/p/13821201.html
Copyright © 2020-2023  润新知