过滤非中英文的特殊字符
/*** 过滤特殊字符* @param str str* @return String*/
public static String filterStr(String str) {if (StringUtils.isBlank(str)) {return str;}String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+·|{}【】‘;:”“’。,、?]";Pattern p = Pattern.compile(regEx);Matcher m = p.matcher(str);return m.replaceAll("").trim();
}
过滤表情字符
<dependency><groupId>com.vdurmont</groupId><artifactId>emoji-java</artifactId><version>3.1.3</version></dependency>
/*** 获取非表情字符串** @param input String* @return String*/public static String getNonEmojiString(String input) {int prev = 0;StringBuilder sb = new StringBuilder();List<EmojiParser.UnicodeCandidate> replacements = getUnicodeCandidates(input);for (EmojiParser.UnicodeCandidate candidate : replacements) {sb.append(input.substring(prev, candidate.getEmojiStartIndex()));prev = candidate.getFitzpatrickEndIndex();}return sb.append(input.substring(prev)).toString();}
hutool工具的EmojiUtil也是基于emoji-java的Emoji表情工具类实现的
使用
public static void main(String[] args) {System.out.println(filterStr("(*+﹏+*)~@"));System.out.println("\uD83D\uDE01");System.out.println(cn.hutool.extra.emoji.EmojiUtil.removeAllEmojis("31dfd电饭\uD83D\uDE011231dfd电饭锅"));System.out.println(getNonEmojiString("31dfd电饭\uD83D\uDE011231dfd电饭锅"));}