栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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

1 maven引入emoji-java

  com.vdurmont
  emoji-java
  5.1.1

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;


@Slf4j
public class EmojiTool extends EmojiParser {

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

    
    public static Set getEmojiEndPos(String str) {
        if (StringUtils.isBlank(str)) {
            return Sets.newHashSet();
        }
        return getEmojiEndPos(str.toCharArray());
    }

    public static void main(String[] args) {
        String str = "这个杀手不太冷";
        Set 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.*;


public class StringTool {
   //用户名称长度少于等于这个数字则不模糊 
   private int USERNAME_NOT_BLUR_NUM = 3;

    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 <= USERNAME_NOT_BLUR_NUM) {
            return str;
        }
        //字符串头节点不模糊的字符数
        int index = 1;
        //字符串尾节点不模糊的字符数
        int end = 1;
        //字符串尾节点最终保留的字符数(两个*和一个普通字符)
        int removeIndex = 3;
        Set emojiPosSet = EmojiTool.getEmojiEndPos(str);
        //存在表情包(一个表情包占两个字符)
        if (CollectionUtils.isNotEmpty(emojiPosSet)) {
            int first = 2;
            //第一个字符是表情包
            boolean head = emojiPosSet.contains(first);
            if (head) {
                //表情包占两个字符,所以字符串头节点不模糊的字节数为2
                index = 2;
            }
            //中间字符包含表情包
            boolean middle = false;
            for (int i = first+1;i 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/743052.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号