Skip to content

Instantly share code, notes, and snippets.

@Acvrock
Created May 29, 2018 11:53
Show Gist options
  • Save Acvrock/b10685d4e379381f05a8d0d2eb73d49f to your computer and use it in GitHub Desktop.
Save Acvrock/b10685d4e379381f05a8d0d2eb73d49f to your computer and use it in GitHub Desktop.
import org.apache.commons.lang.StringUtils;
public class EmojiFilter {
public EmojiFilter() {
}
public static boolean containsEmoji(String source) {
if (StringUtils.isBlank(source)) {
return false;
} else {
int len = source.length();
for(int i = 0; i < len; ++i) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
return true;
}
}
return false;
}
}
private static boolean isEmojiCharacter(char codePoint) {
return codePoint == 0 || codePoint == '\t' || codePoint == '\n' || codePoint == '\r' || codePoint >= ' ' && codePoint <= '\ud7ff' || codePoint >= '\ue000' && codePoint <= '�' || codePoint >= 65536 && codePoint <= 1114111;
}
public static String filterEmoji(String source) {
if (!containsEmoji(source)) {
return source;
} else {
StringBuilder buf = null;
int len = source.length();
for(int i = 0; i < len; ++i) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
if (buf == null) {
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
}
}
if (buf == null) {
return source;
} else if (buf.length() == len) {
buf = null;
return source;
} else {
return buf.toString();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment