Chinaunix首页 | 论坛 | 博客
  • 博客访问: 164979
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2018-06-29 15:34:26


字符串中表情处理,  聊天工具中常用到
包含:
  • 判断字符串中是否含有表情
  • 过滤字符串中的emoji表情

点击(此处)折叠或打开

  1. /**
  2.  */
  3. public class EmojiUtil {
  4.     /**
  5.      * 判断字符串中是否含有表情
  6.      * @param source
  7.      * @return
  8.      */
  9.     public static boolean containsEmoji(String source) {
  10.         if(StringUtil.isEmpty(source)){
  11.             return false;
  12.         }
  13.         int len = source.length();
  14.         boolean isEmoji = false;
  15.         for (int i = 0; i < len; i++) {
  16.             char hs = source.charAt(i);
  17.             if (0xd800 <= hs && hs <= 0xdbff) {
  18.                 if (source.length() > 1) {
  19.                     char ls = source.charAt(i + 1);
  20.                     int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
  21.                     if (0x1d000 <= uc && uc <= 0x1f77f) {
  22.                         return true;
  23.                     }
  24.                 }
  25.             } else {
  26.                 // non surrogate
  27.                 if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
  28.                     return true;
  29.                 } else if (0x2B05 <= hs && hs <= 0x2b07) {
  30.                     return true;
  31.                 } else if (0x2934 <= hs && hs <= 0x2935) {
  32.                     return true;
  33.                 } else if (0x3297 <= hs && hs <= 0x3299) {
  34.                     return true;
  35.                 } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
  36.                     return true;
  37.                 }
  38.                 if (!isEmoji && source.length() > 1 && i < source.length() - 1) {
  39.                     char ls = source.charAt(i + 1);
  40.                     if (ls == 0x20e3) {
  41.                         return true;
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.         return isEmoji;
  47.     }

  48.     /**
  49.      * 顾虑字符串中的emoji表情
  50.      * @param source
  51.      * @return
  52.      */
  53.     public static String filterEmoji(String source) {
  54.         if(StringUtil.isEmpty(source)){
  55.             return source;
  56.         }
  57.         String newStr = "";
  58.         int len = source.length();
  59.         for (int i = 0; i < len; i++) {
  60.             char hs = source.charAt(i);
  61.             if (0xd800 <= hs && hs <= 0xdbff) {
  62.                 if (source.length() > 1) {
  63.                     char ls = source.charAt(i + 1);
  64.                     int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
  65.                     if (0x1d000 <= uc && uc <= 0x1f77f) {
  66.                         i ++;
  67.                         continue;
  68.                     }
  69.                 }
  70.             } else {
  71.                 // non surrogate
  72.                 if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
  73.                     continue;
  74.                 } else if (0x2B05 <= hs && hs <= 0x2b07) {
  75.                     continue;
  76.                 } else if (0x2934 <= hs && hs <= 0x2935) {
  77.                     continue;
  78.                 } else if (0x3297 <= hs && hs <= 0x3299) {
  79.                     continue;
  80.                 } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
  81.                     continue;
  82.                 }
  83.                 if (source.length() > 1 && i < source.length() - 1) {
  84.                     char ls = source.charAt(i + 1);
  85.                     if (ls == 0x20e3) {
  86.                         i++;
  87.                         continue;
  88.                     }
  89.                 }
  90.             }
  91.             newStr = newStr + hs;
  92.         }
  93.         return newStr;
  94.     }
  95. }

阅读(1372) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~