微信emoji表情,微信小程序微信小程序开发用户昵称中带有emoji表情的处理方法
刚开始写小程序,获取客户昵称,后来发现用户昵称是可以为emoji表情的,而Mysql数据库中是不支持直接将emoji表情直接存入数据库的,后来在网站上找了一个方法将所有的昵称转变为Unicode编码存入数据库例如:ud83cudf52这个样子的字符串具体方法为:将str转变为Unicode未解码字符串:publicstaticStringunicode(Stringsource){StringBuffersb=newStringBuffer();char[]source_char=source.toCharArray();Stringunicode=null;for(inti=0;isource_char.length;i++){unicode=Integer.toHexString(source_char[i]);if(unicode.length()=2){unicode="00"+unicode;}sb.append("\u"+unicode);}returnsb.toString()}其中的source就是需要转换的字符串。如果从数据库中获取需要将数据库中的Unicode未解码字符串转变为解码后的字符,具体方法为方法一:publicstaticStringdecodeUnicode(Stringunicode){StringBuffersb=newStringBuffer();String[]hex=unicode.split("\\u");for(inti=1;ihex.length;i++){intdata=Integer.parseInt(hex[i],16);sb.append((char)data);}returnsb.toString();}方法二:publicstaticStringdecodeUnicode2(StringdataStr){intstart=0;intend=0;finalStringBufferbuffer=newStringBuffer();while(start-1){end=dataStr.indexOf("\u",start+2);StringcharStr=null;if(end==-1){charStr=dataStr.substring(start+2,dataStr.length());}else{charStr=dataStr.substring(start+2,end);}charletter=(char)Integer.parseInt(charStr,16);buffer.append(newCharacter(letter).toString());start=end;}returnbuffer.toString();}通过以上两种方法将数据库中的字符串转变成中文字符或者emoji表情但是后来又发现一个问题,就是无法通过模糊查询或者精确查询'ud83cudf52'这样的字符串,具体原因似乎是Mysql会将''舍去,所以selecefromtablewherenick_namelike'%ud83cudf52%'这样查出来的是空的,如果需要查询这样的字符需要将''转变成'_'来进行查询selecefromtablewherenick_namelike'%_ud83c_udf52%'这样就可以查出自己想查询的信息了。原文地址:https://blog.csdn.net/qq_34492495/article/details/79092992
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。

