Skip to content

Instantly share code, notes, and snippets.

@Buttonwood
Created July 29, 2015 03:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Buttonwood/ce0255f39e3d6b14edd7 to your computer and use it in GitHub Desktop.
Save Buttonwood/ce0255f39e3d6b14edd7 to your computer and use it in GitHub Desktop.
表情字符判断
/**
* 判断一个字符是否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