<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.
*/
@Slf4j
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) {
log.error("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 int USERNAME_NOT_BLUR_NUM = 3;
private StringTool() {
}
/**
*
* @param str 字符串
* @param strLen 字符串长度
* @param index 首端不模糊的位置
* @param end 尾端不模糊的位置
* @param removeIndex 尾端留下的字符数
* @return
*/
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<Integer> 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<strLen;i++) {
middle = emojiPosSet.contains(i);
if (middle) {
break;
}
}
//最后一个字符是表情包
boolean tail = emojiPosSet.contains(strLen);
if (tail) {
//表情包占两个字符,所以字符串尾节点不模糊的字节数为2
end = 2;
//表情包占两个字符,字符串尾节点最终保留的字符数为4(两个*和一个表情包)
removeIndex+=1;
}
// 表情包是普通字符数量的2倍
int emojiNum = 2 * USERNAME_NOT_BLUR_NUM;
//strLen == 4 则有两个表情包 或者 一个表情包和两个普通字符
if (strLen == emojiNum - 2) {
boolean fourBool = head || middle || tail;
if (fourBool) {
return str;
}
}
//strLen == 5 则有两个表情包和一个普通字符 有三种组合
boolean fiveBool = (head && middle) || (head && tail) || (middle && tail);
if ( (strLen == emojiNum - 1) && fiveBool) {
return str;
}
//strLen == 6 则有三个表情包
if ( (strLen == emojiNum) && (head && middle && tail)) {
return str;
}
}
return around(str,strLen,index,end,removeIndex);
}
public static void main(String[] args) {
String str = "发看";
String result = around(str);
System.out.println(result);
}
}