ISO Country Code to Unicode Flag in C♯ and JavaScript

Posted July 22nd, 2018, updated March 15th, 2021 in c-sharp

The Unicode 6.0 spec defines Regional Indicator symbols, which includes a set of flags. Here are the founders of NATO, if your system supports the flag characters (Windows 10 doesn't yet):

šŸ‡§šŸ‡Ŗ šŸ‡ØšŸ‡¦ šŸ‡©šŸ‡° šŸ‡«šŸ‡· šŸ‡®šŸ‡ø šŸ‡®šŸ‡¹ šŸ‡±šŸ‡ŗ šŸ‡³šŸ‡± šŸ‡³šŸ‡“ šŸ‡µšŸ‡¹ šŸ‡¬šŸ‡§ šŸ‡ŗšŸ‡ø

To create a flag, take the country ISO code (for example GB), and offset it to the flags Unicode block.

Cā™Æ

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(); // šŸ‡«šŸ‡·

JavaScript

function isoCountryCodeToFlagEmoji(country)
{
   return String.fromCodePoint(...[...country.toUpperCase()].map(c => c.charCodeAt() + 0x1F1A5));
}

let gb = isoCountryCodeToFlagEmoji("gb"); // šŸ‡¬šŸ‡§
let fr = isoCountryCodeToFlagEmoji("fr"); // šŸ‡«šŸ‡·

Tagged flag unicode specification regional indicator symbols nato character windows country

Comments

Please click here to load comments.