国家/地区代码列表 - C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1324048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:09:22  来源:igfitidea点击:

Country codes list - C#

c#internationalizationcountry-codes

提问by Tejaswi Yerukalapudi

I have a string which I need to verify if it's a Country code. The culture is German. Is there any method that I can call to get a list of Country codes in a German culture without having to type out all the 274 (?) codes myself?

我有一个字符串,我需要验证它是否是国家/地区代码。文化是德国的。有没有什么方法可以让我获得德国文化中的国家/地区代码列表,而无需自己输入所有 274 (?) 代码?

Thanks, Teja.

谢谢,泰嘉。

采纳答案by Ostemar

When you say "country code" I assume you mean the two-letter code as in ISO 3166. Then you can use the RegionInfo constructor to check if your string is a correct code.

当您说“国家/地区代码”时,我假设您指的是ISO 3166 中的两个字母代码。然后您可以使用 RegionInfo 构造函数来检查您的字符串是否是正确的代码。

string countryCode = "de";
try {
    RegionInfo info = new RegionInfo(countryCode);
}
catch (ArgumentException argEx)
{
    // The code was not a valid country code
}

You could also, as you state in your question, check if it is a valid country code for the german language. Then you just pass in a specific culture name together with the country code.

正如您在问题中所述,您也可以检查它是否是德语的有效国家/地区代码。然后,您只需传入特定的文化名称和国家/地区代码即可。

string language = "de";
string countryCode = "de";
try {
    RegionInfo info = new RegionInfo(string.Format("{0}-{1}", language, countryCode));
}
catch (ArgumentException argEx)
{
    // The code was not a valid country code for the specified language
}

回答by Agent_9191

If you only need countries/regions, you can make use of the RegionInfo class: http://msdn.microsoft.com/en-us/library/system.globalization.regioninfo.aspx

如果您只需要国家/地区,您可以使用 RegionInfo 类:http: //msdn.microsoft.com/en-us/library/system.globalization.regioninfo.aspx

回答by Kuvalda.Spb.Ru

http://cldr.unicode.org/- common standard multilanguage database include country list and other localizable data.

http://cldr.unicode.org/- 通用标准多语言数据库包括国家列表和其他本地化数据。

回答by Kjata30

The accepted answer is a misuse of the ArgumentExceptionthrown by the constructor. You're not really using the RegionInfoor the ArgumentExceptioninstances, which makes the code's purpose very unclear.

接受的答案是滥用ArgumentException构造函数抛出的。您并没有真正使用RegionInfoArgumentException实例,这使得代码的目的非常不清楚。

Instead, get a list of all specific cultures, then search through the regions of those cultures to find a match on your ISO 3166 alpha-2 code:

相反,获取所有特定文化的列表,然后搜索这些文化的区域以查找与您的 ISO 3166 alpha-2 代码匹配的内容:

bool IsCountryCodeValid(string countryCode)
{
    return CultureInfo
        .GetCultures(CultureTypes.SpecificCultures)
            .Select(culture => new RegionInfo(culture.LCID))
                .Any(region => region.TwoLetterISORegionName == countryCode);
}

Or specifically, for your problem:

或者具体来说,对于您的问题:

bool IsValidGermanCountryCode(string countryCode)
{
    return CultureInfo
        .GetCultures(CultureTypes.SpecificCultures)
            .Where(culture => culture.TwoLetterISOLanguageName == "de")
                .Select(culture => new RegionInfo(culture.LCID))
                    .Any(region => region.TwoLetterISORegionName == countryCode);
}

回答by Alasdair Ross

Be careful when using RegionInfoto check for a valid ISO code. It will return a region if the code you supply is valid AND it is a supported region, but it will not do this for all valid ISO 3166 codes.

使用RegionInfo检查有效的 ISO 代码时要小心。如果您提供的代码有效并且它是受支持的区域,它将返回一个区域,但它不会对所有有效的 ISO 3166 代码执行此操作。

See here for a fuller explanation: https://social.msdn.microsoft.com/Forums/en-US/c9a8bc14-d571-4702-91a6-1b80da239009/question-of-regioninfo-and-region-cy

更完整的解释见这里:https: //social.msdn.microsoft.com/Forums/en-US/c9a8bc14-d571-4702-91a6-1b80da239009/question-of-regioninfo-and-region-cy

RegionInfowill work fine for Europe, but there are several African countries that aren't validated with this method (e.g. Uganda).

RegionInfo将适用于欧洲,但有几个非洲国家未使用此方法进行验证(例如乌干达)。