絵文字表現変換ツール

インポートjava.io.UnsupportedEncodingException; 
インポートjava.net.URLDecoder; 
インポートjava.net.URLEncoder; 
import java.util.regex.Matcher; 
インポートjava.util.regex.Pattern; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

/ ** 
 *絵文字转换工具类
 * 
 * @version $ IDが$ 
 * @since 2018年11月12日午後01時44分
 * / 
publicクラスEmojiUtil {
    静的最終ロガーロガー= LoggerFactory.getLogger(EmojiUtil.class)。

    / ** 
     *絵文字表情转换成字符串
     * 
     * @param strの
     * 
     * @return 
     * 
     * @throwsにUnsupportedEncodingException 
     * /
    public static String emojiEncode(String str)throws UnsupportedEncodingException { 
        String patternString = "([\\ x {10000}-\\ x {10ffff} \ ud800- \ udfff])"; 
        パターンpattern = Pattern.compile(patternString); 
        マッチャーマッチャー= pattern.matcher(str); 
        StringBuffer sb = new StringBuffer(); 
        while(matcher.find()){ 
            try { 
                matcher.appendReplacement(sb、 "[[" + URLEncoder.encode(matcher.group(1)、 "UTF-8")+ "]]"); 
            } catch(UnsupportedEncodingException e){ 
                LOGGER.error( "emojiEncode error"、e); 
                eを投げる; 
            } 
        }
        マッチャー。
        LOGGER.debug( "emojiEncode" + str + "to" + sb.toString()+ "、len:" + sb.length()); 
        sb.toString();を返します。
    } 

    / 
     ***将志字
     文字列転换成絵文字
     * @ param str 
     * * @return 
     * 
     * @throws UnsupportedEncodingException 
     *例外
     * / 
    public static String emojiDecode(String str)throws UnsupportedEncodingException { 
        String patternString = "\\ [\\ [(。*?)\\] \\] "; 

        パターンpattern = Pattern.compile(patternString); 
        マッチャーマッチャー= pattern.matcher(str); 

        StringBuffer sb = new StringBuffer(); 
        while(matcher.find()){
            { 
                matcher.appendReplacement(sb、URLDecoder.decode(matcher.group(1)、 "UTF-8"));を試してください
            } catch(UnsupportedEncodingException e){ 
                LOGGER.error( "emojiDecode error"、e); 
                eを投げる; 
            } 
        } 
        matcher.appendTail(sb); 
        LOGGER.debug( "emojiDecode" + str + "to" + sb.toString()); 
        sb.toString();を返します。
    } 
}

おすすめ

転載: blog.csdn.net/A___B___C/article/details/83992595