字典的简单使用。

对国家个数进行计数。写vba字典写多了,熟悉下c#里面的写法。

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "中国/捷恩斯/日本/中国/日本/中国/韩国/中国/韩国/中国/日本/韩国/中国/日本/中国/日本";
            Dictionary<string, int> d = new Dictionary<string, int>();

            var a = s.Split(new char[]{ '/' });
            foreach (string i in a)
            { if (!d.ContainsKey(i))
                {
                    d.Add(i, 1);
                }
                else
                {
                    d[i]++;
                    
                }
            }
            foreach(KeyValuePair<string,int>kvp in d)
            {
                Console.WriteLine($"{kvp.Key}:{kvp.Value}");
            }
            Console.ReadKey();
        }
    }
}

运行结果:

猜你喜欢

转载自blog.csdn.net/qq_24499417/article/details/104425054