get country code from country name in android

前端 未结 5 1257
旧时难觅i
旧时难觅i 2021-01-05 09:52

I know there is a way to obtain the country name from a country code, but is it also possible the other way arround? I have found so far no function that converts a String l

5条回答
  •  独厮守ぢ
    2021-01-05 10:25

    Not very efficient, but you can try this:

    for (Locale locale : Locale.getAvailableLocales()) {
        if (locale.getDisplayCountry(Locale.US).equals("Netherlands")) {
            // Found !
        }
    }
    

    Now, there are a few caveats:

    • is your input string spelled in english (if not, do you know the language?)?
    • is the country you are looking part of a supported locale (if not this will not work)?

提交回复
热议问题