Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get emoji flag by country code

Tags:

java

android

There is a list of countries codes, I need to attach emoji flag to each one. Is there a way to extract unicode from it or find emoji for country code?

This npm example looks similar for my goal (but uses hexadecimal as input) https://github.com/thekelvinliu/country-code-emoji/blob/master/src/index.js

like image 625
Ardi Avatar asked Feb 14 '17 19:02

Ardi


People also ask

What does the flag mean 🏴?

Published March 30, 2021. The Transgender Flag emoji 🏳️‍⚧️ portrays a flag with pink, blue, and white stripes. This particular flag is used to represent the transgender community and is used to express transgender Pride.

Which country flag is this 🇷 🇺?

Unicode. The Flag of Russia is represented as the Unicode emoji sequence U+1F1F7 🇷 REGIONAL INDICATOR SYMBOL LETTER R and U+1F1FA 🇺 REGIONAL INDICATOR SYMBOL LETTER U, making "🇷🇺".

How do I get a country flag on my Android?

You can get the flag of a country by using the two iso alpha2 or alpha3 or the country name or the numeric code.


2 Answers

This code snippet worked for me. Just replace "US" with whichever valid country code (based on the Regional Indicator Symbol Let­ters) you like and it will create a String flag containing the flag emoji for that country. (Reference)

int flagOffset = 0x1F1E6;
int asciiOffset = 0x41;

String country = "US";

int firstChar = Character.codePointAt(country, 0) - asciiOffset + flagOffset;
int secondChar = Character.codePointAt(country, 1) - asciiOffset + flagOffset;

String flag = new String(Character.toChars(firstChar))
            + new String(Character.toChars(secondChar));

This answer helped

like image 87
Carl Poole Avatar answered Oct 06 '22 10:10

Carl Poole


        function getFlagEmoji(countryCode)
        {
            const codePoints = 
                countryCode
                    .toUpperCase()
                    .split('')
                    .map( char => 127397 + char.charCodeAt() );

            return String.fromCodePoint(...codePoints);
        }

       console.log(getFlagEmoji( 'PK' ));
like image 28
Arshad Ali Avatar answered Oct 06 '22 10:10

Arshad Ali