### Find all emoji in a string Source: https://github.com/sigpwned/emoji4j/blob/main/README.md This example demonstrates how to use GraphemeMatcher to find all emoji within a given text string. ```java GraphemeMatcher m=new GraphemeMatcher(text); while(m.find()) { System.out.println("Found grapheme "+m.grapheme().getName()); } ``` -------------------------------- ### Find only emoji (as opposed to all image graphemes) Source: https://github.com/sigpwned/emoji4j/blob/main/README.md This example shows how to filter the found graphemes to include only those of type EMOJI, excluding other image graphemes. ```java GraphemeMatcher m=new GraphemeMatcher(text); while(m.find()) { if(m.grapheme().getType() == Grapheme.Type.EMOJI) { System.out.println("Found emoji "+m.grapheme().getName()); } } ``` -------------------------------- ### Replace all emoji with their names Source: https://github.com/sigpwned/emoji4j/blob/main/README.md This snippet shows how to replace all detected emoji in a string with their corresponding names using a lambda expression. ```java text = new GraphemeMatcher(text).replaceAll(mr -> mr.grapheme().getName()); ``` -------------------------------- ### Is string all emojis? Source: https://github.com/sigpwned/emoji4j/wiki/Cookbook Checks if a string consists entirely of emoji characters. ```Java /** * @param input The string to test * @return {@code true} if the given string is made up entirely of graphemes, or {@code false} otherwise. * The empty string is considered to be all emoji. */ public static boolean isStringAllEmoji(String input) { AtomicBoolean result = new AtomicBoolean(input.isEmpty()); newGraphemeMatcher(input).results().forEachOrdered(new Consumer() { private boolean onto = true; private int lastEnd = 0; @Override public void accept(GraphemeMatchResult m) { if (m.start() != lastEnd) onto = false; lastEnd = m.end(); result.set(onto && lastEnd == input.length()); } }); return result.get(); } // isStringAllEmoji("") β†’ true // isStringAllEmoji("πŸ‘©πŸ‘©πŸΌπŸ‘©πŸ½πŸ‘©πŸΎπŸ‘©πŸΏ") β†’ true // isStringAllEmoji("πŸ‘© πŸ‘©πŸΌ πŸ‘©πŸ½ πŸ‘©πŸΎ πŸ‘©πŸΏ") β†’ false // isStringAllEmoji("Hello, world!") β†’ false ``` -------------------------------- ### Count emoji in string Source: https://github.com/sigpwned/emoji4j/wiki/Cookbook Counts the number of emoji characters in a given string. ```Java /** * @param input The string to count * @return The number of emoji in the given string */ public static long countEmoji(String input) { return newGraphemeMatcher(input).results().count(); } // countEmoji("alpha bravo charlie") β†’ 0L // countEmoji("alpha πŸ™‚ bravo πŸ™‚ charlie") β†’ 2L ```