如何获得国家名称

发布于 2024-09-18 04:17:43 字数 478 浏览 6 评论 0原文

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

谢谢

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

示例输出:

西班牙语(波多黎各)

西班牙语(美国)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

書生途 2024-09-25 04:17:43

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

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

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);
}
鹿港小镇 2024-09-25 04:17:43

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

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

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);
}
黯淡〆 2024-09-25 04:17:43
// 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");
// 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");
雨巷深深 2024-09-25 04:17:43

这就是您正在寻找的:

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

this would be what you are looking for:

foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
    sb.Append(ci.EnglishName);
    sb.AppendLine();
}
澉约 2024-09-25 04:17:43

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

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

You will need to use following namespaces

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"));
}
二手情话 2024-09-25 04:17:43

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

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

You can use my nuget package Nager.Country. There is a lot of additional information available for each country. For more information please visit the 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文