java 用户名称中emoji包情包的模糊处理

阅读 47

2022-02-14

1 maven引入emoji-java

<dependency>
  <groupId>com.vdurmont</groupId>
  <artifactId>emoji-java</artifactId>
  <version>5.1.1</version>
</dependency>

2 表情包工具类

import com.dianping.cat.Cat;
import com.google.common.collect.Sets;
import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;

/**
 * 表情包静态工具
 * Created by mazhen on 2022-02-22.
 */
public class EmojiTool extends EmojiParser {

    public static Set<Integer> getEmojiEndPos(char[] text) {
        Set<Integer> set = Sets.newHashSet();
        for(int i = 0; i < text.length; ++i) {
            Integer emojiEnd = -1;
            try {
                emojiEnd = getEmojiEndPos(text, i);
            } catch (Exception e) {
                Cat.logError("Emoji position exception",e);
            }
            if (emojiEnd >= 0) {
                set.add(emojiEnd);
            }
        }
        return set;
    }

    /**
     * 表情包出现的位置
     * @param str 包含表情包的字符串
     * @return
     */
    public static Set<Integer> getEmojiEndPos(String str) {
        if (StringUtils.isBlank(str)) {
            return Sets.newHashSet();
        }
        return getEmojiEndPos(str.toCharArray());
    }

    public static void main(String[] args) {
        String str = "😊这个杀手不太冷😭";
        Set<Integer> list =  getEmojiEndPos(str);
        System.out.println(list);
    }

}

3 emoji包情包的模糊处理工具类

import com.util.emoji.EmojiTool;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 字符串静态工具
 * 
 * Created by mazhen on 2022-02-22.
 */
public class StringTool {
    
   

    private StringTool() {
    }
    

    private static String around (String str, int strLen, int index, int end, int removeIndex) {
        String leftStr = StringUtils.left(str,index);
        String rightStr = StringUtils.right(str,end);
        String leftPadStr = StringUtils.leftPad(rightStr,strLen,"*");
        String removeStr = leftPadStr.length() <= removeIndex ? leftPadStr : leftPadStr.substring(strLen-removeIndex);
        String resultStr = leftStr.concat(removeStr);
        return resultStr;
    }

    public static String around (String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        int strLen = StringUtils.length(str);
        if (strLen <= 2) {
            return str;
        }
        int index = 1;
        int end = 1;
        int removeIndex = 3;
        Set<Integer> emojiPosSet = EmojiTool.getEmojiEndPos(str);
        if (CollectionUtils.isNotEmpty(emojiPosSet)) {
            if (emojiPosSet.contains(2)) {
                index = 2;
            }
            if (emojiPosSet.contains(strLen)) {
                end = 2;
                removeIndex +=1;
            }
            if (strLen <= 5) {
                removeIndex = strLen-index;
            }
        }
        String resultStr = around(str,strLen,index,end,removeIndex);
        return resultStr;
    }

    public static void main(String[] args) {
        String str = "发看😂";
        String result = around(str);
        System.out.println(result);
    }
}

精彩评论(0)

0 0 举报