Created
October 2, 2022 07:28
-
-
Save prongbang/e89b9ef634d0ac33c9683305ee9213f1 to your computer and use it in GitHub Desktop.
Convert Unicode to Emoji for Android
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.regex.Pattern | |
// How to use | |
// | |
// val actual = EmojiConverter.fromUnicode("เพชรพลอย U+1F48E 8920 เพชรพลอย U+1F48E 8920") // เพชรพลอย 💎 เพชรพลอย 💎 8920 | |
// emojiTextView.setText(EmojiCompat.get().process(actual)) | |
// | |
object EmojiConverter { | |
fun fromUnicode(textUnicode: String): String { | |
val pattern = Pattern.compile("(U\\+[A-F0-9]+)") | |
return try { | |
textUnicode.replace(pattern.toRegex()) { result -> | |
StringBuilder() | |
.appendCodePoint(Integer.decode(result.value.replace("U+", "0x"))) | |
.toString() | |
} | |
} catch (e: Exception) { | |
textUnicode | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment