存儲(chǔ)emoji

再項(xiàng)目中遇到了emoji存儲(chǔ)的問(wèn)題,苦于后臺(tái)已經(jīng)有不少數(shù)據(jù),想要置換說(shuō)是難度很大,所以存儲(chǔ)的問(wèn)題落到了前端身上。

查了一些資料,網(wǎng)上比較普遍的做法還是數(shù)據(jù)庫(kù)升級(jí),但是也有不少資料可供參考,其中用到的思想就是正則表達(dá)式和講emoji轉(zhuǎn)化為對(duì)應(yīng)十六進(jìn)制對(duì)應(yīng)的字符串。這里轉(zhuǎn)化16進(jìn)制是utf-8編碼的十六進(jìn)制編碼,主要是為了配合ios端。使用這種方法,可以將emoji變?yōu)閿?shù)據(jù)庫(kù)可以存儲(chǔ)的字符串。本來(lái)做了就做了,后來(lái)同事將網(wǎng)上沒有看到類似的方法,所以想到還是放到網(wǎng)上來(lái)吧。源碼放下邊

這里是github地址



···

public class EmojiUtils {

public static final Stringpatch ="[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]";

? ? public static final Stringpatch2 ="\\[+[0-9a-fA-F]+]";

? ? /**

* 將含有4字節(jié)字符的字符轉(zhuǎn)為固定格式

*

? ? * @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();

? ? }

/**

* 將含有固定格式的字符串轉(zhuǎn)化為四字節(jié)字符

*

? ? * @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("轉(zhuǎn)換之前的十六進(jìn)制" + count);

? ? ? ? ? ? count = count.substring(1, count.length() -1);

? ? ? ? ? ? matcher.appendReplacement(sb, hexStringToString(count));

? ? ? ? }

matcher.appendTail(sb);

? ? ? ? LogUtil.showTime(msg +"emoji");

? ? ? ? return sb.toString();

? ? }

/**

* 16進(jìn)制字符串轉(zhuǎn)換為字符串

*

? ? * @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;

? ? }

}

···

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容