将数据中的(表情)emoji标签转换成utf8字符集,存入与读取数据库

emoji标签转换utf—8字符集工具类

1、将emoji标签转换成utf8字符集保存进数据库

     * 将emoji标签转换成utf8字符集保存进数据库
     * @param str
     * @return
     */
    public static String emojiConvert(String str) {
        String patternString = "([\\x{10000}-\\x{10ffff}\\ud800-\\udfff])";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            try {
                matcher.appendReplacement(sb,"[[" + URLEncoder.encode(matcher.group(1),"UTF-8") + "]]");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

2、 还原utf8数据库中保存的含转换后emoji表情的字符串

    /**
     * 还原utf8数据库中保存的含转换后emoji表情的字符串
     * @param str
     * @return
     */
 public static String emojiRecovery(String str) {
        String patternString = "\\[\\[(.*?)\\]\\]";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            try {
                matcher.appendReplacement(sb,
                        URLDecoder.decode(matcher.group(1), "UTF-8"));
            } catch(UnsupportedEncodingException e) {
                return "";
            }
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

你可能感兴趣的:(java)