java转换emoji表情

摘要:
投掷器;Matchermatcher=模式匹配器(str);StringBuffersb=newStringBuffer();}}匹配者附加尾部(某人);returnsb.toString();

/**
* @Description 将字符串中的emoji表情转换成可以在utf-8字符集数据库中保存的格式(表情占4个字节,需要utf8mb4字符集)
* @param str
* 待转换字符串
* @return 转换后字符串
* @throws UnsupportedEncodingException
* exception
*/
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) {
LOG.error("emojiConvert error", e);
throw e;
}
}
matcher.appendTail(sb);
LOG.debug("emojiConvert " + str + " to " + sb.toString()
+ ", len:" + sb.length());
return sb.toString();
}

/**
* @Description 还原utf8数据库中保存的含转换后emoji表情的字符串
* @param str
* 转换后的字符串
* @return 转换前的字符串
* @throws UnsupportedEncodingException
* exception
*/
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) {
LOG.error("emojiRecovery error", e);
throw e;
}
}
matcher.appendTail(sb);
LOG.debug("emojiRecovery " + str + " to " + sb.toString());
return sb.toString();
}

免责声明:文章转载自《java转换emoji表情》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Vue简单基础 + 实例 及 组件通信4G EPS 中的 PDN Connection下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

HttpURLConnection使用POST方法参数乱码

如题,HttpURLConnection使用POST方法发起http请求,参数通过form来传递(并非使用URL传递参数),出现了中文乱码的情况。 具体描述为:将请求参数以 Content-Disposition: form-data; name="name" value 形式上传,然后使用OutputStream.write传送,结果中文参数会出现...

.NetCore3.1获取文件并重新命名以及大批量更新及写入数据

using Microsoft.AspNetCore.Mvc; using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using...