The emoji emoji is also encoded using Unicode, but UTF8 encoding is not supported. If we want to store emoji to the database, there are generally two methods. Take mysql as an example to change the database encoding from utf8 to utf8mb4. The second is to make a conversion and convert the emoji expression to another character. So this article will mainly talk to you about how to use Java to process emoji expressions.
xml
<!-- /artifact//emoji-java --> <dependency> <groupId></groupId> <artifactId>emoji-java</artifactId> <version>5.1.1</version> </dependency>
Java code
import ; import ; import ; import ; import ; import ; import ; /** * emoji filter * @author madaha * */ public class EmojiFilter { /** * Determine whether there is Emoji * @author madaha * * @param codePoint * @return */ private static boolean isEmojiCharacter(char codePoint) { return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)); } /** * Filter emoji or other non-literal characters * @author madaha * * @param source string to be filtered * @return */ public static String filterEmoji(String source) { if ((source)) { return source; } StringBuilder buf = null; int len = (); for (int i = 0; i < len; i++) { char codePoint = (i); if (isEmojiCharacter(codePoint)) { if (buf == null) { buf = new StringBuilder(()); } (codePoint); } } if (buf == null) { return source; } else { if (() == len) { buf = null; return source; } else { return (); } } } /** * @Description Convert the emoji in the string into a format that can be saved in the utf-8 character set database (the emoji takes up 4 bytes, and the utf8mb4 character set is required) * @param str string to be converted * @return Converted string * @throws UnsupportedEncodingException * exception */ public static String emojiConvert1(String str) throws UnsupportedEncodingException { String patternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])"; Pattern pattern = (patternString); Matcher matcher = (str); StringBuffer sb = new StringBuffer(); while (()) { try { (sb, "[[" + ((1), "UTF-8") + "]]"); } catch (UnsupportedEncodingException e) { (e); throw e; } } (sb); // ("emojiConvert " + str + " to " + () + ", len:" + ()); return (); } /** * @Description Restore the string containing converted emoji expressions saved in the utf8 database * @param str String before conversion * @return Converted string * @throws UnsupportedEncodingException * exception */ public static String emojiRecovery2(String str) throws UnsupportedEncodingException { String patternString = "\\[\\[(.*?)\\]\\]"; Pattern pattern = (patternString); Matcher matcher = (str); StringBuffer sb = new StringBuffer(); while (()) { try { (sb, ((1), "UTF-8")); } catch (UnsupportedEncodingException e) { (e); throw e; } } (sb); // ("emojiRecovery " + str + " to " + ()); return (); } /** * Test method for emoji expression processing * @author madaha * * @param args */ public static void main(String[] args) { String str = "emoji expression😁😁Enter test😁😁"; ("The original character is:\n" + str); ("to aliases after:\n" + (str)); str = (str); ("Restore:\n" + (str)); } }
This is the article about writing emoji emoji processing tools based on Java. For more related Java emoji processing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!