文章内容
1、表情列表
public static final String EMOJI_STRING = "[微笑][撇嘴][色][发呆][得意][流泪][害羞][闭嘴][睡][大哭][尴尬][发怒][调皮][呲牙][惊讶][难过][囧][抓狂][吐][偷笑][愉快][白眼][傲慢][困][惊恐][流汗][憨笑][悠闲][奋斗][咒骂][疑问][嘘][晕][衰][骷髅][敲打][再见][擦汗][抠鼻][鼓掌][坏笑][左哼哼][右哼哼][哈欠][鄙视][委屈][快哭了][阴险][亲亲][可怜][菜刀][西瓜][啤酒][咖啡][蛋糕][米饭][药][奶瓶][棒棒糖][礼物][红包][奖状][100分][五角星][气球][小风车][鞭炮][灯笼][玩具熊][发][汽车][发财猫][猪头][狗头][猫头][玫瑰][凋谢][嘴唇][爱心][心碎][炸弹][便便][月亮][太阳][星星][拥抱][公主抱][强][弱][握手][胜利][抱拳][勾引][拳头][OK][戴口罩][笑出泪][惊吓][难受][嘿哈][捂脸][奸笑][机智][皱眉][耶][打脸][鬼][祈祷][雪人][公主][国王][王后]";
2、长度计算(表情占2个长度)
public static int count(String str) {
int count = 0;
if (StringUtil.isNotEmpty(str)) {
int pIndex = -1;
int index = 0;
int offset = 0;
while (index < str.length()) {
char ch = str.charAt(index);
offset = 0;
if (ch == '[') {
if (pIndex != -1) {
offset += index - pIndex;
}
if (index != str.length() - 1) {
pIndex = index;
} else {
offset++;
}
} else if (ch == ']') {
if (pIndex != -1) {
if (pIndex == index - 1) {
offset += 2;
} else {
if (EMOJI_STRING.contains(str.substring(pIndex, index + 1))) {
offset += 2;
} else {
offset += index - pIndex + 1;
}
}
pIndex = -1;
} else {
offset++;
}
} else if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
|| (ch >= 'u4e00' && ch <= 'u9fa5')) {
if (pIndex == -1) {
offset++;
}
} else {
if (pIndex != -1) {
offset += index - pIndex + 1;
pIndex = -1;
} else {
offset++;
}
}
count += offset;
index++;
}
}
return count;
}
3、字符串裁剪
public static String substring(String str, int length) {
String substring = "";
int count = 0;
if (StringUtil.isNotEmpty(str)) {
int pIndex = -1;
int index = 0;
int offset = 0;
String lastStr = "";
while (index < str.length()) {
char ch = str.charAt(index);
offset = 0;
lastStr = "";
if (ch == '[') {
if (pIndex != -1) {
offset += index - pIndex;
}
if (index != str.length() - 1) {
pIndex = index;
} else {
offset++;
}
} else if (ch == ']') {
if (pIndex != -1) {
if (pIndex == index - 1) {
offset += 2;
} else {
if (EMOJI_STRING.contains(str.substring(pIndex, index + 1))) {
lastStr = str.substring(pIndex, index + 1);
offset += 2;
} else {
offset += index - pIndex + 1;
}
}
pIndex = -1;
} else {
offset++;
}
} else if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
|| (ch >= 'u4e00' && ch <= 'u9fa5')) {
if (pIndex == -1) {
offset++;
}
} else {
if (pIndex != -1) {
offset += index - pIndex + 1;
pIndex = -1;
} else {
offset++;
}
}
count += offset;
if (count == length) {
substring = str.substring(0, index + 1);
break;
} else if (count > length) {
if (StringUtil.isNotEmpty(lastStr)) {
substring = str.substring(0, index + 1 - lastStr.length());
} else {
substring = str.substring(0, index - (count - length));
}
break;
}
index++;
}
}
return substring;
}