因项目要保存用户微信昵称,其中多含emoji表情,mysql版本低于5.5.3无法保存(不敢升),只有转换为unicode和过滤两种解决方法,搜得过滤的文章一篇,稍作改动,自测通过
原文地址:https://www.oschina.net/question/89964_105220
1 import org.apache.commons.lang.StringUtils; 2 3 public class EmojiUtil { 4 5 /** 6 * 检测是否有emoji字符 7 * @param source 8 * @return 9 */ 10 public static boolean containsEmoji(String source) { 11 if (StringUtils.isBlank(source)) { 12 return false; 13 } 14 15 int len = source.length(); 16 17 for (int i = 0; i < len; i++) { 18 char codePoint = source.charAt(i); 19 20 if (isEmojiCharacter(codePoint)) { 21 //do nothing,判断到了这里表明,确认有表情字符 22 return true; 23 } 24 } 25 26 return false; 27 } 28 29 private static boolean isNotEmojiCharacter(char codePoint) { 30 return (codePoint == 0x0) || 31 (codePoint == 0x9) || 32 (codePoint == 0xA) || 33 (codePoint == 0xD) || 34 ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || 35 ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || 36 ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)); 37 } 38 39 private static boolean isEmojiCharacter(char codePoint) { 40 return !isNotEmojiCharacter(codePoint); 41 } 42 43 /** 44 * 过滤emoji 45 * @param source 46 * @return 47 */ 48 public static String filterEmoji(String source) { 49 if (!containsEmoji(source)) { 50 return source; 51 } 52 StringBuilder sb = new StringBuilder(); 53 int len = source.length(); 54 for (int i = 0; i < len; i++) { 55 char c = source.charAt(i); 56 if (isEmojiCharacter(c)) { 57 continue; 58 } 59 sb.append(c); 60 } 61 return sb.toString(); 62 } 63 64 /*** 65 * 提取文本中的emoji表情 66 * @param source 67 * @return 68 */ 69 public static String extractEmoji(String source) { 70 if (!containsEmoji(source)) { 71 return null; 72 } 73 StringBuilder sb = new StringBuilder(); 74 int len = source.length(); 75 for (int i = 0; i < len; i++) { 76 char codePoint = source.charAt(i); 77 if (isEmojiCharacter(codePoint)) { 78 sb.append(codePoint); 79 } 80 } 81 return sb.toString(); 82 } 83 84 public static void main(String[] args) { 85 String s = "神奇{6}66Abc!"; 86 String emoji = UnicodeUtil.unicode2String("\\u4d\\u6f\\u4d\\u6f\\u9171\\ud83c\\udf51"); 87 System.out.println(s + "是否包含emoji: " + containsEmoji(s)); 88 System.out.println(emoji + "是否包含emoji: " + containsEmoji(emoji)); 89 System.out.println(s + "过滤emoji后: " + filterEmoji(s)); 90 System.out.println(emoji + "过滤emoji后: " + filterEmoji(emoji)); 91 System.out.println(s + "提取出的emoji: " + extractEmoji(s)); 92 System.out.println(emoji + "提取出的emoji: " + extractEmoji(emoji)); 93 } 94 }