国家代码清单 - C#
当你说“国家代码”我想你指的是两个字母的代码作为ISO 3166。然后你可以使用RegionInfo构造函数来检查你的字符串是否是正确的代码。
string countryCode = "de";
try {
RegionInfo info = new RegionInfo(countryCode);
}
catch (ArgumentException argEx)
{
// The code was not a valid 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
}
如果你只需要国家/地区,您可以使用RegionInfo类: http://msdn.microsoft.com/en-us/library/system.globalization.regioninfo.aspx
接受的答案是滥用了构造函数抛出的ArgumentException
。你并没有真正使用RegionInfo
或ArgumentException
实例,这使得代码的目的很不明确。
取而代之的是,让所有特定文化的列表,然后通过搜索这些文化的地区找到你的ISO 3166的α-2码匹配:
bool IsCountryCodeValid(string countryCode)
{
return CultureInfo
.GetCultures(CultureTypes.SpecificCultures)
.Select(culture => new RegionInfo(culture.LCID))
.Any(region => region.TwoLetterISORegionName == countryCode);
}
或者具体而言,对于您的问题:
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);
}
显然这使用LINQ。 – Kjata30 2015-01-28 22:56:35
使用RegionInfo
检查有效的ISO代码时要小心。如果您提供的代码有效并且它是受支持的区域,它将返回一个区域,但它不会为所有有效的ISO 3166代码执行此操作。
在这里看到一个更全面的解释:https://social.msdn.microsoft.com/Forums/en-US/c9a8bc14-d571-4702-91a6-1b80da239009/question-of-regioninfo-and-region-cy
RegionInfo
将罚款欧洲,但也有未用此方法(例如乌干达)验证的几个非洲国家。
事实证明,德国健康保险使用不同的命名国家系统。国家代码大小从1-3个字符变化。感谢您的帮助! - Teja 0秒前 – 2009-08-24 20:55:35
哇......真奇怪。如果只有三个字母,我会猜测它是ISO 3166-1 Alpha 3,国家代码使用三个字母(德语为DEU)。 – Ostemar 2009-08-24 21:00:21
这很奇怪; ISO 3166国家代码是大写。 – rds 2012-10-30 09:38:26