Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert two-letter country codes to flag emojis?

I have ISO 3166-1 (alpha-2) country codes, which are two-letter codes, such as "US" and "NL". How do I get the corresponding flag emoji?

EDIT: Preferably I would like to do this without using an explicit mapping between country codes and their corresponding emojis. It has been done in JavaScript but I'm not sure how to do it in C#.

like image 281
Rudey Avatar asked Nov 13 '17 19:11

Rudey


People also ask

Is there a Greenflag emoji?

This emoji is named "green flag" and is licensed under the open source Creative Commons Attribution 4.0 International license. It's a colored emoji. It's also a defined emoji, which means it's part of the open standard on emojis.

Are there emojis for state flags?

While U.S. state flags won't be added by the Unicode Consortium, vendors are free to design their own flag emoji for states and other subdivisions.


1 Answers

Solution:

public static string IsoCountryCodeToFlagEmoji(this string country)
{
    return string.Concat(country.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));
}

string gb = "gb".IsoCountryCodeToFlagEmoji(); // 🇬🇧
string fr = "fr".IsoCountryCodeToFlagEmoji(); // 🇫🇷
like image 156
M. Hamza Rajput Avatar answered Oct 05 '22 01:10

M. Hamza Rajput