【Android】過濾字符串中的Emoji表情

在網上找了一部分,而後根據本身的項目進行了改動。 1.沒有采用自定義的表情發送模式,而是直接套用的Android系統裏面的Emoji表情。 2.在表情發送的時候,也沒有采用Unicode編碼方式。由於項目緣由,而是採用了以下簡單的方式。編程

但願能夠幫助你們,直接上使用的代碼:數組

//提交以前的數據,使表情編程數字數組 [-16, -97, -104, -95]
    String string;
    public String filterEmoji(String source) {
        if (!containsEmoji(source)) {
            return source;// 若是不包含,直接返回
        }
        StringBuilder buf = null;
        int len = source.length();
        System.out.println("filter running len = " + len);
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (buf == null) {
                buf = new StringBuilder(source.length());
            }
            if (!isEmojiCharacter(codePoint)) {
                string = String.valueOf(codePoint);
            } else {
                try {
                    StringBuilder builder = new StringBuilder(2);
                    byte[] str = builder.append(String.valueOf(codePoint))
                            .append(String.valueOf(source.charAt(i+1)))
                            .toString().getBytes("UTF-8");
                    String strin = Arrays.toString(str);
                    String newString = strin.substring(1, strin.length() - 1);
                    string = "Γ"+newString+"Γ";
                    System.out.println("filters running newStr = " + string);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                i++;
            }
            buf.append(string+"⅞");
        }
        if (buf == null) {
            return "";
        } else {
            if (buf.length() == len) {// 這裏的意義在於儘量少的toString,由於會從新生成字符串
                buf = null;
                return source;
            } else {
                System.out.println("filter running buf.toString() = " + buf.toString());
                String bufStr = buf.toString();
                String newBufStr= bufStr.substring(0, bufStr.length() - 1);
                return newBufStr;
            }
        }
    }

    // 判別是否包含Emoji表情
    private boolean containsEmoji(String str) {
        int len = str.length();
        for (int i = 0; i < len; i++) {
            if (isEmojiCharacter(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    private 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)));
    }

    //獲得服務器的數據以後進行解析,顯示在UI上
    String newsString;
    public void setString(TextView textView,String string) {
        StringBuilder stringBuilder = new StringBuilder();
        String arrays[] = string.split("⅞");
        for (int j = 0; j < arrays.length; j++) {
            System.out.println("filter running arrays[] = "+arrays[j]);
            String  ss = arrays[j];
            char char_ss = ss.charAt(0);
            System.out.println("filter running String.valueOf(char_ss) = "+String.valueOf(char_ss));
            if (String.valueOf(char_ss).equals("Γ")){
                String new_SS = ss.substring(1, ss.length() - 1);
                String strArrays[] = new_SS.split(", ");
                byte[] chars = new byte[strArrays.length];
                for (int i = 0; i < strArrays.length; ++i) {
                    System.out.println("strArrays[i]:" + strArrays[i]);
                    chars[i] = Byte.decode(strArrays[i]);
                }
                newsString = new String(chars);
            }else{
                newsString =ss;
            }
            stringBuilder.append(newsString);
            System.out.println("filter running stringBuilder.toString() = "+stringBuilder.toString());
            textView.setText(stringBuilder.toString());
        }
    }

這是我在項目中使用過的方法,但願能夠幫助你們,若是有什麼好的建議和意見,歡迎留言。服務器

相關文章
相關標籤/搜索