For example, we want to put the nickname into the database, we need to filter out the expression.
Package Com.zjr.common.utils;import Org.apache.commons.lang3.stringutils;public class Emojifilter {/** * detects if there are emoji characters * * @param source * @return throws */public static Boolean Containsemoji (String source) {if (Stringutils.isblank (source)) once it contains ) {return false;} int len = Source.length (), for (int i = 0; i < len; i++) {Char codepoint = Source.charat (i); if (Isemojicharacter (codepoi NT)) {//Do nothing, judging here indicates that there is an emoticon character return true;}} return false;} private static Boolean isemojicharacter (char codepoint) {return (codepoint = = 0x0) | | (codepoint = = 0x9) | | (codepoint = = 0xA) | | (codepoint = = 0xD) | | ((codepoint >= 0x20) && (codepoint <= 0xd7ff)) | | ((codepoint >= 0xe000) && (codepoint <= 0xFFFD)) | | ((codepoint >= 0x10000) && (codepoint <= 0x10ffff));} /** * Filter emoji or other non-literal type characters * * @param source * @return */public static string Filteremoji (string source) {if (!containse Moji (source)) {return source;//if not included, return directly to}//here is definitely included 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;//If no emoji expression is found, return the source string} else {if (buf.length () = = Len) {//The meaning here is as little as ToString, because it will be re-born into string buf = Null;return source;} else {return buf.tostring ();}}}}
Java implementation filters out emoticons in text