Last active
January 2, 2025 04:04
Kotlin way of converting country codes to emoji flags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Locale | |
| fun countryCodeToEmojiFlag(countryCode: String) { | |
| return countryCode | |
| .toUpperCase(Locale.US) | |
| .map { char -> | |
| Character.codePointAt("$char", 0) - 0x41 + 0x1F1E6 | |
| } | |
| .map { codePoint -> | |
| Character.toChars(codePoint) | |
| } | |
| .joinToString(separator = "") { charArray -> | |
| String(charArray) | |
| } | |
| } |
Thanks for your contribution. Usage:
countryCodeToEmojiFlag("CO") // --> 🇨🇴
(do not use the country code phone number, like me hehe)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, very useful!
Two small corrections: Declare return type
: String, and useuppercase()sincetoUpperCase()has been deprecated.