utf-8 문자 집합 닉네임 특수 표정 처리 포함

1829 단어 바꾸다
아래 두 가지 처리 방법 을 사용 하면 된다.
 /**
     * @param str       
     * @return       
     * @throws UnsupportedEncodingException exception
     * @Description       emoji        utf-8            
     */
    public static String emojiConvert1(String str) throws UnsupportedEncodingException {
        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) {
                throw e;
            }
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @param str        
     * @return        
     * @throws UnsupportedEncodingException exception
     * @Description   utf8           emoji      
     */
    public static String emojiRecovery2(String str) throws UnsupportedEncodingException {
        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) {
                throw e;
            }
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

좋은 웹페이지 즐겨찾기