JAVA工具類之表情及特殊字符過濾

EmojiUtils

package com.lh.wx.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.util.TextUtils;

public class EmojiUtils {
    public static String filter(String str){
        if(str == null || str.length() == 0){
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<str.length();i++){
            int ch = str.charAt(i);
            int min = Integer.parseInt("E001", 16);
            int max = Integer.parseInt("E537", 16);
            if(ch >= min && ch <= max){
                sb.append("");
            }else{
                sb.append((char)ch);
            }
        }
        return sb.toString();
    }

    /**
     * 過濾暱稱特殊表情
     */
    public static String filterName(String name) {
        if(name==null){
            return null;

        }
        if("".equals(name.trim())){
            return "";
        }

        Pattern patter = Pattern.compile("[a-zA-Z0-9\u4e00-\u9fa5]");
        Matcher match = patter.matcher(name);

        StringBuffer buffer = new StringBuffer();
        boolean flag=false;
        while (match.find()) {
            buffer.append(match.group());
        }
        if(!name.equals(buffer.toString())){
            flag=true;
        }
        if(flag)
        return "?"+buffer.toString();   
        else
        return buffer.toString();
    }
    /**
     * 將字符串中的Emoji表情轉換成Unicode編碼
     * @param src
     * @return
     */
    public static  String emoji2Unicode(String src) {
        StringBuffer unicode = new StringBuffer();

        for (int i = 0; i < src.length(); i++) {
            char c = src.charAt(i);
            int codepoint = src.codePointAt(i);
            if(isEmojiCharacter(codepoint)) {
                unicode.append("\\u").append(Integer.toHexString(c));
            } else {
                unicode.append(c);
            }
        }
        return unicode.toString();
    }
    /**
     * 判斷是否包含Emoji符號
     * @param codePoint
     * @return
     */
    public static boolean isEmojiCharacter(int codePoint) {
        return (codePoint >= 0x2600 && codePoint <= 0x27BF) // 雜項符號與符號字體
                || codePoint == 0x303D
                || codePoint == 0x2049
                || codePoint == 0x203C
                || (codePoint >= 0x2000 && codePoint <= 0x200F)//
                || (codePoint >= 0x2028 && codePoint <= 0x202F)//
                || codePoint == 0x205F //
                || (codePoint >= 0x2065 && codePoint <= 0x206F)//
                /* 標點符號佔用區域 */
                || (codePoint >= 0x2100 && codePoint <= 0x214F)// 字母符號
                || (codePoint >= 0x2300 && codePoint <= 0x23FF)// 各種技術符號
                || (codePoint >= 0x2B00 && codePoint <= 0x2BFF)// 箭頭A
                || (codePoint >= 0x2900 && codePoint <= 0x297F)// 箭頭B
                || (codePoint >= 0x3200 && codePoint <= 0x32FF)// 中文符號
                || (codePoint >= 0xD800 && codePoint <= 0xDFFF)// 高低位替代符保留區域
                || (codePoint >= 0xE000 && codePoint <= 0xF8FF)// 私有保留區域
                || (codePoint >= 0xFE00 && codePoint <= 0xFE0F)// 變異選擇器
                || codePoint >= 0x10000; // Plane在第二平面以上的,char都不可以存,全部都轉
    }
    /**
    * 將Unicode字符轉成中文
    * @param src
    * @return
    */
   public static String unicode2Emoji(String src) {
       if (TextUtils.isEmpty(src)) {
           return "";
       }

       StringBuffer retBuf = new StringBuffer();
       int maxLoop = src.length();
       for (int i = 0; i < maxLoop; i++) {
           if (src.charAt(i) == '\\') {
               if ((i < maxLoop - 5) && ((src.charAt(i + 1) == 'u') || (src.charAt(i + 1) == 'U'))) {
                   try {
                       retBuf.append((char) Integer.parseInt(src.substring(i + 2, i + 6), 16));
                       i += 5;
                   } catch (NumberFormatException localNumberFormatException) {
                       retBuf.append(src.charAt(i));
                   }
               } else {
                   retBuf.append(src.charAt(i));
               }
           } else {
               retBuf.append(src.charAt(i));
           }
       }
       return retBuf.toString();
   }
}
發佈了79 篇原創文章 · 獲贊 14 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章