Skip to content

Instantly share code, notes, and snippets.

@biezhi
Forked from Buttonwood/Emoji.java
Created Apr 19, 2016
Embed
What would you like to do?
表情字符判断
/**
* 判断一个字符是否emoji表情字符
*
* @param ch
* 待检测的字符
*/
public static boolean isEmoji(char ch) {
return !((ch == 0x0) || (ch == 0x9) || (ch == 0xA) || (ch == 0xD)
|| ((ch >= 0x20) && (ch <= 0xD7FF))
|| ((ch >= 0xE000) && (ch <= 0xFFFD)) || ((ch >= 0x10000) && (ch <= 0x10FFFF)));
}
/**
* 清除一个字符串中的emoji表情字符
*/
public static String cleanEmoji(String s) {
if (isEmpty(s)) {
return null;
}
StringBuilder builder = new StringBuilder(s);
for (int i = 0; i < builder.length(); i++) {
if (isEmoji(builder.charAt(i))) {
builder.deleteCharAt(i);
builder.insert(i, ' ');// 比字符串中直接替换字符要好,那样会产生很多字符串对象
}
}
return builder.toString().trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment