Skip to content

Instantly share code, notes, and snippets.

@bhurling
Last active September 14, 2022 04:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhurling/c955c778f7a0765aaffd9214b12b3963 to your computer and use it in GitHub Desktop.
Save bhurling/c955c778f7a0765aaffd9214b12b3963 to your computer and use it in GitHub Desktop.
Kotlin way of converting country codes to emoji flags
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)
}
}
@tillkuhn
Copy link

Thanks, very useful!
Two small corrections: Declare return type : String, and use uppercase() since toUpperCase() has been deprecated.

fun countryCodeToEmojiFlag(countryCode: String): String {
    return countryCode
        .uppercase(Locale.US)
        .map { char ->
          Character.codePointAt("$char", 0) - 0x41 + 0x1F1E6
        }
        .map { codePoint ->
          Character.toChars(codePoint)
        }
        .joinToString(separator = "") { charArray ->
          String(charArray)
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment