【问题标题】:How to get country name如何获得国家名称
【发布时间】:2010-09-07 04:19:38
【问题描述】:

我使用下面的代码来获取文化类型列表,有没有办法只获取国家名称?

谢谢

static void Main(string[] args)
{
    StringBuilder sb = new StringBuilder();
    foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
    {
        sb.Append(ci.DisplayName);
        sb.AppendLine();
    }
    Console.WriteLine(sb.ToString());
    Console.ReadLine();
}

样本输出:

西班牙语(波多黎各)

西班牙语(美国)

【问题讨论】:

    标签: c#-4.0 globalization cultureinfo country


    【解决方案1】:

    您可以使用 CultureInfo 的 Name 属性来构造 RegionInfo。然后,您可以使用 DisplayName 属性。 试试:

    foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
    {
        var ri = new RegionInfo(ci.Name);
        Console.WriteLine(ri.DisplayName);
    }
    

    【讨论】:

    • 你应该使用new RegionInfo(ci.LCID),它更快。来源:反编译器。 MSDN 上的链接:CultureInfo.LCIDRegionInfo constructor
    • 如果您有自定义文化,请注意使用 LCID - 它们都有 LCID = 4096。
    【解决方案2】:

    嗯,这个正则表达式似乎在大多数情况下都能胜任:

    var regex = new System.Text.RegularExpressions.Regex(@"([\w+\s*\.*]+\))");
    foreach (var item in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
    {
        var match = regex.Match(item.DisplayName);
        string countryName = match.Value.Length == 0 ? "NA" : match.Value.Substring(0, match.Value.Length - 1);
        Console.WriteLine(countryName);
    }
    

    【讨论】:

    • DisplayName 给出了“德国”、“加泰罗尼亚”、“芬兰”等名称。这些并不完全是国家名称。否则,我们可能会使用 DisplayName 或 EnglishName。
    • 在大多数情况下,DisplayName 包括用括号括起来的国家/地区名称,这是我们使用正则表达式获得的最后一部分。有点hack,但它有效:-)
    • 显示名称不是国家/地区
    【解决方案3】:
    // Build your normal dictionary as container
    Dictionary<string, string> countryNames = new Dictionary<string, string>();
    foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
    {
        RegionInfo ri = new RegionInfo(ci.Name);
        // Check if the ISO language code is already in your collection 
        // Because you don't want double entries in a country box because we had to use the culture info
        if (!countryNames.ContainsKey(ri.TwoLetterISORegionName))
        {
            countryNames.Add(ri.TwoLetterISORegionName.ToUpper(), ri.EnglishName);
        }
    }
    // Code for dictionary to dropdownlist transform can be written with your personal preference for symantics 
    SelectList countryDropdown = new SelectList(countryNames.OrderBy(o => o.Value), "Key", "Value");
    

    或无需 cmets 即可使用:

    Dictionary<string, string> countryNames = new Dictionary<string, string>();
    foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
    {
        RegionInfo ri = new RegionInfo(ci.Name);
        if (!countryNames.ContainsKey(ri.TwoLetterISORegionName)) countryNames.Add(ri.TwoLetterISORegionName.ToUpper(), ri.EnglishName);
    }
    SelectList countryDropdown = new SelectList(countryNames.OrderBy(o => o.Value), "Key", "Value");
    

    【讨论】:

    • 使用 Keys.ToList().Contains() 是个坏主意,因为它使算法 O(n^2)。为什么不直接使用containsKey()
    【解决方案4】:

    这就是你要找的东西:

    foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
    {
        sb.Append(ci.EnglishName);
        sb.AppendLine();
    }
    

    【讨论】:

      【解决方案5】:

      您将需要使用以下命名空间

      using System.Configuration; 
      using System.Globalization;       
      
      /// <summary>
      /// populate country name
      /// </summary>
      /// <param name="dropDown"></param>
      public static void GetCountryNames(DropDownList dropDown)
      {
          Hashtable h = new Hashtable();
          Dictionary<string, string> objDic = new Dictionary<string, string>();
          foreach (CultureInfo ObjCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
          {
              RegionInfo objRegionInfo = new RegionInfo(ObjCultureInfo.Name);
              if (!objDic.ContainsKey(objRegionInfo.EnglishName))
              {
                  objDic.Add(objRegionInfo.EnglishName, objRegionInfo.TwoLetterISORegionName.ToLower());
              }
          }
      
          SortedList<string, string> sortedList = new SortedList<string, string>(objDic);
          foreach (KeyValuePair<string, string> val in sortedList)
          {
              dropDown.Items.Add(new ListItem(val.Key, val.Key));
          }
      
          dropDown.Items.Insert(0, new ListItem("Select", "Select"));
          dropDown.Items.Insert(1, new ListItem("Other Country", "Other"));
      }
      

      【讨论】:

        【解决方案6】:

        你可以使用我的 nuget 包Nager.Country。每个国家/地区都有很多附加信息。欲了解更多信息,请访问Github project

        PM> install-package Nager.Country
        
        ICountryProvider countryProvider = new CountryProvider();
        foreach (var countryCode in (Alpha2Code[])Enum.GetValues(typeof(Alpha2Code)))
        {
            var countryInfo = countryProvider.GetCountry(countryCode);
            Console.WriteLine(countryInfo.CommonName);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-05
          • 2020-09-13
          • 2016-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多