存储emoji
再项目中遇到了emoji存储的问题,苦于后台已经有不少数据,想要置换说是难度很大,所以存储的问题落到了前端身上。
查了一些资料,网上比较普遍的做法还是数据库升级,但是也有不少资料可供参考,其中用到的思想就是正则表达式和讲emoji转化为对应十六进制对应的字符串。这里转化16进制是utf-8编码的十六进制编码,主要是为了配合ios端。使用这种方法,可以将emoji变为数据库可以存储的字符串。本来做了就做了,后来同事将网上没有看到类似的方法,所以想到还是放到网上来吧。源码放下边
···
public class EmojiUtils {
public static final Stringpatch ="[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]";
public static final Stringpatch2 ="\\[+[0-9a-fA-F]+]";
/**
* 将含有4字节字符的字符转为固定格式
*
* @param msg
* @return
*/
public static StringgetEmoji(String msg) {
LogUtil.showTime(msg +"emoji2");
Pattern pattern = Pattern.compile(patch);
Matcher matcher = pattern.matcher(msg);
StringBuffer sb =new StringBuffer();
try {
while (matcher.find()) {
byte[] count;
count = URLDecoder.decode(matcher.group(), "UTF-8").getBytes();
String cha ="[";
for (int i =0; i < count.length; i++) {
String s = Integer.toHexString(count[i]);
if (s.length() >2) {
s = s.substring(s.length() -2);
}
cha += s;
}
cha +="]";
matcher.appendReplacement(sb, cha);
}
}catch (Exception e) {
e.printStackTrace();
}
matcher.appendTail(sb);
LogUtil.showTime(msg +"emoji2");
return sb.toString();
}
/**
* 将含有固定格式的字符串转化为四字节字符
*
* @param msg
* @return
*/
public static StringgetString(String msg) {
LogUtil.showTime(msg +"emoji");
Pattern pattern = Pattern.compile(patch2);
Matcher matcher = pattern.matcher(msg);
StringBuffer sb =new StringBuffer();
while (matcher.find()) {
String count = matcher.group();
// LogUtil.show("转换之前的十六进制" + count);
count = count.substring(1, count.length() -1);
matcher.appendReplacement(sb, hexStringToString(count));
}
matcher.appendTail(sb);
LogUtil.showTime(msg +"emoji");
return sb.toString();
}
/**
* 16进制字符串转换为字符串
*
* @param s
* @return
*/
public static StringhexStringToString(String s) {
if (s ==null || s.equals("")) {
return null;
}
s = s.replace(" ", "");
byte[] baKeyword =new byte[s.length() /2];
for (int i =0; i < baKeyword.length; i++) {
try {
baKeyword[i] = (byte) (Integer.parseInt(
s.substring(i *2, i *2 +2), 16));
}catch (Exception e) {
e.printStackTrace();
}
}
try {
s =new String(baKeyword, "UTF-8");
}catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
}
···