How to get country name

懵懂的女人 提交于 2019-12-04 16:04:56

问题


I used the code below to get the list of culture type, is there a way on how to get just the country name?

Thank you

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


    }

Sample Output:

Spanish (Puerto Rico)

Spanish (United States)


回答1:


Well, this regular expression seems to do the job in most cases:

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



回答2:


You can use the Name property of the CultureInfo to construct a RegionInfo. You can then use the DisplayName property. Try:

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

Or ready for use without comments:

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



回答4:


this would be what you are looking for:

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



回答5:


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

    }

}


来源:https://stackoverflow.com/questions/3655738/how-to-get-country-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!