Created
July 29, 2015 03:54
-
-
Save Buttonwood/ce0255f39e3d6b14edd7 to your computer and use it in GitHub Desktop.
表情字符判断
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 判断一个字符是否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