Java转义emoji等特殊符号
写在前面
网上找了很多转emoji等方法,大多有两种方法
- 更改数据库编码格式为utf8mb4
- 过滤字符串中的emoji
都不是很优雅
- 更改数据库编码,势必影响其他数据库
- 过滤emoj效率比较低
处理Emoji方式
这里推荐使用org.apache.commons.lang3.StringEscapeUtils工具类,简单等两行代码实现特殊符号和emoji表情的转义存储,和读取反转;
转义存储
- StringEscapeUtils.escapeXXX(content)
它有几种转码方式,可以根据个人格式进行选择:
- public static final String escapeCsv(final String input);
- public static final String escapeEcmaScript(final String input);
- public static final String escapeHtml3(final String input);
- public static final String escapeHtml4(final String input);
- public static final String escapeJava(final String input);
- public static final String escapeJson(final String input);
- public static final String escapeXml(final String input);
- public static String escapeXml10(final String input);
- public static String escapeXml11(final String input)
读取反转义
读取后,根据个人格式进行反转义,即可还原emoji值,供前端展示;
- public static final String unescapeCsv(final String input) ;
- public static final String unescapeEcmaScript(final String input);
- public static final String unescapeHtml3(final String input);
- public static final String unescapeHtml4(final String input);
- public static final String unescapeJava(final String input);
- public static final String unescapeJson(final String input);
- public static final String unescapeXml(final String input);
附加一段手打的复杂代码:
package utils; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; public class EmojiUtils {
/**
* emoji表情替换
*
* @param source 原字符串
*
* @param slipStr emoji表情替换成的字符串
*
* @return 过滤后的字符串
*/
public static String filterEmoji(String source, String slipStr) {
if (StringUtils.isNotBlank(source)) {
return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", slipStr);
} else {
return source;
}
} /**
* @Description 将字符串中的emoji表情转换成可以在utf-8字符集数据库中保存的格式(表情占4个字节,需要utf8mb4字符集)
* @param str
* 待转换字符串
* @return 转换后字符串
* @throws UnsupportedEncodingException
* exception
*/
public static String emojiConvert(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) {
throw e;
}
}
matcher.appendTail(sb);
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) {
throw e;
}
}
matcher.appendTail(sb);
return sb.toString();
}
}
Java转义emoji等特殊符号的更多相关文章
- php + mysql 存入表情 【如何转义emoji表情,让它可以存入utf8的数据库】
方法1:base_encode64 这种方法是可以,但是旧数据没有经过encode操作,取数据的时候如果统一进行decode的话,旧数据会丢失的. 1 方法2:urlencode 这个似乎可以,对没有 ...
- Java转义符\\|
http://blog.csdn.net/stewen_001/article/details/22871737 看一段程序 String t = "a||b||c||d"; St ...
- 浅谈Java转义符\\|
看一段程序 String t = "a||b||c||d"; String[] temp = t.split("\\|\\|"); System.out.pri ...
- java 转义符
java 转义符 CreationTime--2018年7月12日15点33分 Author:Marydon 1.常见转义符 转义符在java中有特殊含义 转义字符 特殊含义 \b 退格( ...
- 【maven】【IDEA】idea中使用maven编译项目,报错java: 错误: 找不到符号 【2】
=================================================================================== idea中使用maven编译项目 ...
- Java处理emoji
1.问题产生情况 我遇到这个问题是做微信开发的时候有些有用的头像用了微信的emoji表情,然而我的mysql数据库用的编码是utf8_general_ci,就是utf-8编码,结果也就报错误了. 2. ...
- java转义xml中的多余尖括号
xml中的敏感字符是尖括号,如果xml的值中含有尖括号,那么在解析的时候就会报错,如: <?xml version="1.0" encoding="UTF-8&qu ...
- java过滤emoji表情(成功率高)
转载自:http://blog.csdn.net/huangchao064/article/details/53283738 基本能过滤大部分的ios,安卓,微信emoji表情 有很多别的帖子搜出来很 ...
- java转义符和正则表达式转义符
举例来说,连续相同的3位数字的正则表达式的标准语法是: ([\d])\1{2} 但是如果在java代码中这么写,就会出现语法错误,如下: String regEx = "([\d])\1{2 ...
随机推荐
- shell 命令 bc linux下的计算器
bc命令 在linux环境下的计算器.
- 《A computer-aided healthcare system for cataract classification and grading based on fundus image analysis》学习笔记
Abstract This paper presents a fundus image analysis based computer aided system for automatic class ...
- CentOS7安装BugFree
1. 安装apache yum install httpd 2. 安装mysql yum install mysql yum install mysql-server 注意: 1)已安装mysql的跳 ...
- MCU_数码管常用表
共阴极数码管编码(0---F) unsigned char code table[]={ 0x3f,0x06,0x5d,0x4f, 0x66,0x6d,0x77,0x7c, 0x39,0x5e,0x7 ...
- 「PKUSC2018」最大前缀和(状压dp)
前言 考试被\(hyj\)吊着打... Solution 考虑一下如果前缀和如果在某一个位置的后面的任意一个前缀和都<=0,肯定这就是最大的. 然后这样子就考虑左右两边的状压dp,然后就好了. ...
- Android sharedUserId 和系统权限
sharedUserId 给不同的应用使用同一个 sharedUserId 可以运行在这几个应用间互相访问数据(数据库,SharedPreferences,文件). sharedUserId 一旦使用 ...
- Dubbo原理实现之使用Javassist字节码结束构建代理对象
JavassistProxyFactory利用自己吗技术构建代理对象的实现如下: public <T> T getProxy(Invoker<T> invoker, Class ...
- Redis中的批量操作Pipeline
大多数情况下,我们都会通过请求-相应机制去操作redis.只用这种模式的一般的步骤是,先获得jedis实例,然后通过jedis的get/put方法与redis交互.由于redis是单线程的,下一次请求 ...
- 修改windows远程默认端口
修改windows远程默认端口 windows端口修改rdp 1 远程服务器运行窗口调出注册表编辑器 注册表编辑器regeidt 2 修改两个注册表 1,在注册表HKEY_LOCAL_MACHINE\ ...
- javaScript中BOM
BOM是browser object model的缩写,简称浏览器对象模型 主要处理浏览器窗口(window)和框架(iframe),简述了与浏览器进行交互的方法和接口, 可以对浏览器窗口进行访问和操 ...