Last active
October 23, 2024 14:59
-
-
Save bhurling/c955c778f7a0765aaffd9214b12b3963 to your computer and use it in GitHub Desktop.
Kotlin way of converting country codes to emoji flags
This file contains 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.