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"?
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 .
MY is the two-letter country abbreviation for Malaysia.
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).
TELEPHONY_SERVICE); String countryCode = tm. getSimCountryIso();
//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);
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")
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With