如何获得国家名称

问题描述 投票:17回答:5

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

谢谢

        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
5个回答
3
投票

嗯,这个正则表达似乎在大多数情况下都能完成这项工作:

        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);
        }

50
投票

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

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

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");

或者没有评论就可以使用了:

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");

0
投票

这将是你正在寻找的:

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

0
投票
     You will need to use following namespaces 

     using System.Configuration; 
     using System.Globalization;       

///

    /// 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"));

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.