欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

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

时间:2023-05-02
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;@Slf4jpublic 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

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。