Fundamentals 4 min read

Explore Java 21’s New Emoji Detection APIs

Java 21 introduces six static methods in java.lang.Character that let developers reliably detect emojis, emoji presentations, modifiers, modifier bases, components, and extended pictographic characters using Unicode code points, with practical code examples and regex usage.

Programmer DD
Programmer DD
Programmer DD
Explore Java 21’s New Emoji Detection APIs

Java 21 adds six new static methods to java.lang.Character for detecting Emoji characters.

public static boolean isEmoji(int codePoint) { return CharacterData.of(codePoint).isEmoji(codePoint); }
public static boolean isEmojiPresentation(int codePoint) { return CharacterData.of(codePoint).isEmojiPresentation(codePoint); }
public static boolean isEmojiModifier(int codePoint) { return CharacterData.of(codePoint).isEmojiModifier(codePoint); }
public static boolean isEmojiModifierBase(int codePoint) { return CharacterData.of(codePoint).isEmojiModifierBase(codePoint); }
public static boolean isEmojiComponent(int codePoint) { return CharacterData.of(codePoint).isEmojiComponent(codePoint); }
public static boolean isExtendedPictographic(int codePoint) { return CharacterData.of(codePoint).isExtendedPictographic(codePoint); }

Each method receives a Unicode code point and returns a boolean indicating whether the character matches the specific Emoji property.

Example: using isEmoji to check if a string contains any Emoji.

@Test
void testEmoji() {
    String message = "欢迎来到 www.didispace.com ❤️,要不要来杯 ☕️ ?";
    if (message.codePoints().anyMatch(Character::isEmoji)) {
        System.out.println("Message包含表情");
    }
}

Additional examples show how to determine if an Emoji can be modified or has been modified using isEmojiModifier and isEmojiModifierBase.

@Test
void testEmoji2() {
    String welcomeMsg = "欢迎来到 www.didispace.com ❤️,要不要来杯 ☕️ ?";
    OptionalInt emojiOptional = welcomeMsg.codePoints().filter(Character::isEmoji).findFirst();
    if (emojiOptional.isPresent()) {
        int emojiCodePoint = emojiOptional.getAsInt();
        if (Character.isEmojiModifierBase(emojiCodePoint)) {
            System.out.println("Emoji can be modified");
            if (Character.isEmojiModifier(emojiCodePoint))
                System.out.println("Emoji is modified");
            else
                System.out.println("Emoji has not been modified");
        } else {
            System.out.println("Emoji cannot be modified");
        }
    } else {
        System.out.println("No emoji present");
    }
}

The new methods can also be used via regular expressions, for example:

String welcomeMsg = "欢迎来到 www.didispace.com ❤️,要不要来杯 ☕️ ?";
Matcher matcher = Pattern.compile("\\p{IsEmoji}").matcher(welcomeMsg);
if (matcher.find()) {
    System.out.println("Message contains an emoji!");
}
matcher = Pattern.compile("\\p{IsEmoji_Modifier_Base}").matcher(welcomeMsg);
if (matcher.find()) {
    System.out.println("Message contains an emoji modifier base!");
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

EmojiUnicode
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.