tqt-java

java去除Emoji表情

方式一:

自己创建工具类

public class EmojiFilterUtil {
    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));
    }
    public static String filterEmoji(String source) {
        if (StringUtils.isBlank(source)) {
            return source;
        }
        StringBuilder buf = null;
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (isEmojiCharacter(codePoint)) {
                if (buf == null) {
                    buf = new StringBuilder(source.length());
                }
                buf.append(codePoint);
            }
        }
        if (buf == null) {
            return source;
        } else {
            if (buf.length() == len) {
                buf = null;
                return source;
            } else {
                return buf.toString();
            }
        }
    }

方拾二:

引入maven依赖

<dependency>
   <groupId>com.vdurmont</groupId>
   <artifactId>emoji-java</artifactId>
   <version>5.1.1</version>
</dependency>
关键方法:
EmojiManager.containsEmoji(s);//判断是否包含emoji
EmojiParser.removeAllEmojis(s);//去除所有emoji
EmojiParser.parseToAliases(s);//将emoji转化为别名
EmojiParser.parseToUnicode(s);//转化为unicode

posted on 2022-11-08 14:09  TQT*  阅读(907)  评论(0)    收藏  举报

导航