Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ISO country code from country name

Tags:

c#

Is there a way to get the ISO ALPHA-2 code (country code) from a country name such as United Kingdom = GB?

I'm trying to achieve the nearly the opposite of the code below

//To get the Country Names from the CultureInfo

 foreach (CultureInfo cul in CultureInfo.GetCultures(CultureTypes.SpecificCultures))

 {    
       country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);    
       countryNames.Add(country.DisplayName.ToString());    
 }
like image 490
alwaysVBNET Avatar asked Mar 15 '18 23:03

alwaysVBNET


People also ask

How do you find the country code of a country?

name = locale. getDisplayCountry(); // Map all country names and codes in key - value pairs. countryMap. put(name, code); } // Return the country code for the given country name using the map. // Here you will need some validation or better yet // a list of countries to give to user to choose from.

Can ISO country code?

Country Code CAN Country code according to ISO-3166 Alpha-3 CAN is the three-letter country abbreviation for Canada.

How do I get a country phone prefix ISO?

I got the country iso by the code from this answer: TelephonyManager tm = (TelephonyManager)getSystemService(Context. TELEPHONY_SERVICE); String countryCode = tm. getSimCountryIso();


2 Answers

You can do something like this:

var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID));
var englishRegion = regions.FirstOrDefault(region => region.EnglishName.Contains("United Kingdom"));
var countryAbbrev = englishRegion.TwoLetterISORegionName;
like image 190
darkfall Avatar answered Oct 16 '22 06:10

darkfall


This Class is used to you collect information on Countries and related Cultures.

Its Constructor lets you choose if you want to load all possible Countries or just the Specific Cultures:

CountryList countries = new CountryList([true][false]);

where true means CultureTypes.AllCultures and false means CultureTypes.SpecificCultures

For example, whith these parameters:

CountryList countries = new CountryList(true);
List<CountryInfo> countryInfo = countries.GetCountryInfoByName("United States", false);

(true/false in GetCountryInfoByName() mean use/don't the Native Name)

this method returns three results:

1 Country - United States
3 Cultures - English, English (United States), Spanish (United States)

Using the Native Name:

List<CountryInfo> countryInfo = Countries.GetCountryInfoByName("United States", true);

1 Country - United States
2 Cultures - English, English (United States)

With Specific Cultures and Native Names:

CountryList countries = new CountryList(false);
List<CountryInfo> countryInfo = countries.GetCountryInfoByName("United States", true);

1 Country - United States
1 Culture - English (United States)

More related to your question, this Class exposes these methods:

string twoLettersName = countries.GetTwoLettersName("United States", true);

Returns US

string threeLettersName = countries.GetThreeLettersName("United States", true);

Returns USA

List<string> ietfTags = countries.GetIetfLanguageTag("United States", true);

Returns en-US

List<int> geoIds = countries.GetRegionGeoId("United States", true);

Returns 244

public class CountryList {
    private CultureTypes cultureType;
    public CountryList(bool AllCultures)
    {
        cultureType = AllCultures ? CultureTypes.AllCultures : CultureTypes.SpecificCultures;
        Countries = GetAllCountries(cultureType);
    }

    public List<CountryInfo> Countries { get; set; }

    public List<CountryInfo> GetCountryInfoByName(string CountryName, bool NativeName)
    {
        return NativeName ? Countries.Where(info => info.Region?.NativeName == CountryName).ToList()
                          : Countries.Where(info => info.Region?.EnglishName == CountryName).ToList();
    }

    public List<CountryInfo> GetCountryInfoByName(string CountryName, bool NativeName, bool IsNeutral)
    {
        return NativeName ? Countries.Where(info => info.Region?.NativeName == CountryName &&
                                                    info.Culture?.IsNeutralCulture == IsNeutral).ToList()
                          : Countries.Where(info => info.Region?.EnglishName == CountryName &&
                                                    info.Culture?.IsNeutralCulture == IsNeutral).ToList();
    }

    public string? GetTwoLettersName(string CountryName, bool NativeName)
    {
        CountryInfo? country = NativeName ? Countries.Where(info => info.Region?.NativeName == CountryName).FirstOrDefault()
                                          : Countries.Where(info => info.Region?.EnglishName == CountryName).FirstOrDefault();

        return country?.Region?.TwoLetterISORegionName;
    }

    public string? GetThreeLettersName(string CountryName, bool NativeName)
    {
        CountryInfo? country = NativeName ? Countries.Where(info => info.Region?.NativeName == CountryName).FirstOrDefault()
                                          : Countries.Where(info => info.Region?.EnglishName == CountryName).FirstOrDefault();

        return country?.Region?.ThreeLetterISORegionName;
    }

    public List<string?>? GetIetfLanguageTag(string CountryName, bool UseNativeName)
    {
        return UseNativeName ? Countries.Where(info => info.Region?.NativeName == CountryName)
                                        .Select(info => info.Culture?.IetfLanguageTag).ToList()
                             : Countries.Where(info => info.Region?.EnglishName == CountryName)
                                        .Select(info => info.Culture?.IetfLanguageTag).ToList();
    }

    public List<int?>? GetRegionGeoId(string CountryName, bool UseNativeName)
    {
        return UseNativeName ? Countries.Where(info => info.Region?.NativeName == CountryName)
                                        .Select(info => info.Region?.GeoId).ToList()
                             : Countries.Where(info => info.Region?.EnglishName == CountryName)
                                        .Select(info => info.Region?.GeoId).ToList();
    }

    private static List<CountryInfo> GetAllCountries(CultureTypes cultureTypes)
    {
        List<CountryInfo> countries = new List<CountryInfo>();

        foreach (CultureInfo culture in CultureInfo.GetCultures(cultureTypes)) {
            if (culture.LCID != 127)
                countries.Add(new CountryInfo() {
                    Culture = culture,
                    Region = new RegionInfo(culture.TextInfo.CultureName)
                });
        }
        return countries;
    }
}

public class CountryInfo {
    public CultureInfo? Culture { get; set; }
    public RegionInfo? Region { get; set; }
}
like image 6
Jimi Avatar answered Oct 16 '22 06:10

Jimi