文章内容
1、转码
public static String emojiConvert(String str) {
String newStr = str;
if (StringUtil.isNotEmpty(str)) {
try {
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()) {
matcher.appendReplacement(sb, "[[" + URLEncoder.encode(matcher.group(1), "UTF-8") + "]]");
}
matcher.appendTail(sb);
newStr = sb.toString();
} catch (Exception e) {
// Do nothing
}
}
return newStr;
}
2、解码
public static String emojiRecovery(String str) {
String newStr = str;
if (StringUtil.isNotEmpty(str)) {
try {
String patternString = "[[(.*?)]]";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, URLDecoder.decode(matcher.group(1), "UTF-8"));
}
matcher.appendTail(sb);
newStr = sb.toString();
} catch (UnsupportedEncodingException e) {
// Do nothing
}
}
return newStr;
}