I need to get the first character of a string, currently what I'm using is:
String firstCharacter = getTitle().substring(0, 1);
where getTitle()
returns a String
that can contain multiple words and also emojis, if the first character is an emoji and I use substring
when I display firstCharacter instead of the emoji I get a question mark because using substring
I cut the emoji characters.
What I want to do is:
if the first word is an emoji retrieve and assign it to firstCharacter
without using substring
;
if the first word is an actual word use the substring
as I'm currently doing;
How can I possibly do it?
EmojiCompat is used to support Emojis in Android. You can initialize it by downloading it or packaging it within your app.
// Initialize with your desired config
EmojiCompat.init(BundledEmojiCompatConfig(context))
// Check if EmojiCompat was successfully loaded
if (EmojiCompat.get().loadState == EmojiCompat.LOAD_STATE_SUCCEEDED) {
EmojiCompat.get().hasEmojiGlyph(yourString)
}
Emojis range can be found here
So, this would result in (not tested):
if(getTitle().substring(0,5)
== ("/[\u2190-\u21FF] | [\u2600-\u26FF] | [\u2700-\u27BF] |
[\u3000-\u303F] | [\u1F300-\u1F64F] | [\u1F680-\u1F6FF]/g")){
firstCharacter = getTitle().substring(0,5);
} else{
firstCharacter = getTitle().substring(0,1);
}