views:

1934

answers:

2

Hi all,

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?

Thanks, Teja.

+2  A: 

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

Agent_9191
+4  A: 

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.

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
}
Marpe
It turns out the German health insurance uses a different system of naming countries. Country code sizes varied from 1-3 chars. Thanks for the help though! – Teja 0 secs ago
Tejaswi Yerukalapudi
Wow..that's really odd. If it was only three letters I would have guessed it to be ISO 3166-1 Alpha 3 that uses three letters for the country code (DEU for German).
Marpe