Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Country name to ISO 3166-2 code

I know how to convert an ISO 3166-2 code to the full English name, e.g. "US" to "United States" by using RegionInfo.

However, how can I do the opposite, i.e. that takes "United States" and returns "US"?

like image 847
jacobz Avatar asked Dec 24 '14 00:12

jacobz


People also ask

What is ISO country code?

The ISO country codes are internationally recognized codes that designate every country and most of the dependent areas a two-letter combination or a three-letter combination; it is like an acronym, that stands for a country or a state. The country code is in use for example for the two-letter suffixes such as .

What is my 2 character country code?

MY is the two-letter country abbreviation for Malaysia.

What is the 2 letter code for Morocco?

MA - Morocco. Remark: Entries followed by "(EH)" are located partially or fully in the territory of Western Sahara (ISO 3166 alpha-2 code element EH).

How do I get a country phone prefix ISO?

TELEPHONY_SERVICE); String countryCode = tm. getSimCountryIso();


2 Answers

//Get the cultureinfo
RegionInfo rInfo = new RegionInfo("us");
string s = rInfo.EnglishName;

//Convert it back
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
 CultureInfo cInfo = cultures.FirstOrDefault(culture => new RegionInfo(culture.LCID).EnglishName == s);
like image 116
Mitja Avatar answered Sep 20 '22 10:09

Mitja


The main idea: take all region objects and select from them one which contains given full name.

var regionFullNames = CultureInfo
                      .GetCultures( CultureTypes.SpecificCultures )
                      .Select( x => new RegionInfo(x.LCID) )
                      ;
var twoLetterName = regionFullNames.FirstOrDefault(
                       region => region.EnglishName.Contains("United States")
                    );
like image 30
yesenin Avatar answered Sep 21 '22 10:09

yesenin