爷的眼睛闪亮
insideDotNet En_summerGarden

public 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) {
logger.error("emojiConvert error", e);
throw e;
}
}
matcher.appendTail(sb);
logger.debug("emojiConvert " + str + " to " + sb.toString()
+ ", len:" + sb.length());
return sb.toString();
}

public 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) {
logger.error("emojiRecovery error", e);
throw e;
}
}
matcher.appendTail(sb);
logger.debug("emojiRecovery " + str + " to " + sb.toString());
return sb.toString();
}

posted on 2016-10-21 13:39  爷的眼睛闪亮  阅读(1164)  评论(0编辑  收藏  举报