5

We are trying to let our application accept emojis. We are trying to limit the emojis to the ones from this list.

So, i am trying to read the unicode value (for example, U+1F600 for ) for emojis - so i can validate that the user entered emoji is from the above list.

can somebody please help me with how to read the unicode value for an emoji? or any other ways i can implement this validation ? using encoded values etc.?

        }  

String actualEmojiString = "";
            for(int i=0; i< actualEmojiString.codePointCount(0, actualEmojiString.length()); i++) {
                int codePoint = actualEmojiString.charAt(i);
                if(Character.isSurrogate(actualEmojiString.charAt(i))) {
                    codePoint = Character.toCodePoint(actualEmojiString.charAt(i), actualEmojiString.charAt(i));
                }
                System.out.println( "HexCode value for emoji: [" +  actualEmojiString.charAt(i) + "] is [" +"U+" + Integer.toHexString(codePoint | 0x10000).substring(0) +"]"
                        + "["+"\\u" + Integer.toHexString(codePoint | 0x10000).substring(1) + "]");

Output:

HexCode value for emoji: [?] is [U+1f03d][\uf03d]
HexCode value for emoji: [?] is [U+190200][\u90200]
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
Casey
  • 53
  • 1
  • 6
  • 3
    Java uses UTF16 strings. Plan accordingly. Also, please look at your question after posting, so that you can click "edit" and fix any formatting mistakes you made: this is not a well formatted question at all. – Mike 'Pomax' Kamermans Jul 29 '19 at 19:51

1 Answers1

8

You are overcomplicating your code by mixing code points and chars. Instead use String.codePoints() method:

"".codePoints().mapToObj(Integer::toHexString).forEach(System.out::println);

will print the unicode values as hex:

1f600
1f602

This however might not be needed at all because Java supports unicode String literals. To check if the text contains an emoji you can simply do:

"".contains("");
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111