Java转义工具类StringEscapeUtils的学习笔记
前言
在java.commons.lang3 的包中有许多方便好用的工具类,类似于处理字符串的StringUtils,处理日期的DateUtil等等,StringEscapeUtils也是其中的一员。
StringEscapeUtils 是在java.commons.lang3的2.0 版本中加入的工具类,在3.6版本中被标注为@deprecated,表明在之后的版本中则为过时状态,之后StringEscapeUtils类被移到java.commons.text包下。
依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-text</artifactId><version>1.8</version></dependency>
功能用途
StringEscapeUtils的主要功能就是为Java,Java Script,Html,XML进行转义与反转义。
- escapeJava(String input) / unescapeJava(String unionCodeString) 将输入字符串转为unicode编码 / 将 unicode字符串转为utf-8 格式的字符串
- escapeHtml4(String input) / unescapeHtml4(String input) 转义/反转义html脚本
- escapeEcmaScript(String input) / unescapeEcmaScript(String input) 转义/反转义js脚本
- escapeXml(String input) / unescapeXml(String input) 转义/反转义xml脚本
除了列出的几个较常用的方法,还有escapeJson(String input) / unescapeJson(String input)、escapeCsv(String input) / unescapeCsv(String input) 等等。
示例
执行例子如下:
import org.apache.commons.text.StringEscapeUtils;
import org.junit.Test;/*** @author liuqian* @date 2018/4/3 16:27*/
public class EscapeTest {@Testpublic void escapeTest() {System.out.println("转义/反转义Java字符串");String javaString = "\"这是Java\":\"字符串\"";System.out.println("原字符串: " + javaString);System.out.print("转义后:");System.out.println(StringEscapeUtils.escapeJava(javaString));System.out.print("反转义后:");System.out.println(StringEscapeUtils.unescapeJava(StringEscapeUtils.escapeJava(javaString)));System.out.println(StringEscapeUtils.unescapeJson(StringEscapeUtils.escapeJava(javaString)));System.out.println(StringEscapeUtils.unescapeJava(StringEscapeUtils.escapeJson(javaString)));System.out.println("-------------------------------------------------------------");System.out.println("转义/反转义Json字符串");String jsonString = "{\"keyword\": \"这是Json字符串\"}";System.out.println("原字符串: " + jsonString);System.out.print("转义后:");System.out.println(StringEscapeUtils.escapeJson(jsonString));System.out.print("反转义后:");System.out.println(StringEscapeUtils.unescapeJson(StringEscapeUtils.escapeJson(jsonString)));System.out.println("-------------------------------------------------------------");//除了html4还有html3等格式System.out.println("转义/反转义Html字符串");String htmlString = "<strong>加粗字符</strong>";System.out.println("原字符串: " + htmlString);System.out.print("转义后:");System.out.println(StringEscapeUtils.escapeHtml4(htmlString));System.out.print("反转义后:");System.out.println(StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(htmlString)));System.out.println("-------------------------------------------------------------");//除了xml10还有xml11等格式System.out.println("转义/反转义xml字符串");String xmlString = "<xml>\"xml字符串\"</xml>";System.out.println("原字符串: " + xmlString);System.out.print("转义后:");System.out.println(StringEscapeUtils.escapeXml10(xmlString));System.out.print("反转义后:");System.out.println(StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeXml10(xmlString)));System.out.println("-------------------------------------------------------------");System.out.println("转义/反转义csv字符串");String csvString = "1997,Ford,E350,\"Super, luxurious truck\"";System.out.println("原字符串: " + csvString);System.out.print("转义后:");System.out.println(StringEscapeUtils.escapeCsv(csvString));System.out.print("反转义后:");System.out.println(StringEscapeUtils.unescapeCsv(StringEscapeUtils.escapeCsv(csvString)));System.out.println("-------------------------------------------------------------");System.out.println("转义/反转义Java Script字符串");String jsString = "<script>alert('1111')</script>";System.out.println("原字符串: " + jsString);System.out.print("转义后:");System.out.println(StringEscapeUtils.escapeEcmaScript(jsString));System.out.print("反转义后:");System.out.println(StringEscapeUtils.unescapeEcmaScript(StringEscapeUtils.escapeEcmaScript(jsString)));}
}
运行结果如下:
转义/反转义Java字符串
原字符串: "这是Java":"字符串"
转义后:\"\u8FD9\u662FJava\":\"\u5B57\u7B26\u4E32\"
反转义后:"这是Java":"字符串"
"这是Java":"字符串"
"这是Java":"字符串"
-------------------------------------------------------------
转义/反转义Json字符串
原字符串: {"keyword": "这是Json字符串"}
转义后:{\"keyword\": \"\u8FD9\u662FJson\u5B57\u7B26\u4E32\"}
反转义后:{"keyword": "这是Json字符串"}
-------------------------------------------------------------
转义/反转义Html字符串
原字符串: <strong>加粗字符</strong>
转义后:<strong>加粗字符</strong>
反转义后:<strong>加粗字符</strong>
-------------------------------------------------------------
转义/反转义xml字符串
原字符串: <xml>"xml字符串"</xml>
转义后:<xml>"xml字符串"</xml>
反转义后:<xml>"xml字符串"</xml>
-------------------------------------------------------------
转义/反转义csv字符串
原字符串: 1997,Ford,E350,"Super, luxurious truck"
转义后:"1997,Ford,E350,""Super, luxurious truck"""
反转义后:1997,Ford,E350,"Super, luxurious truck"
-------------------------------------------------------------
转义/反转义Java Script字符串
原字符串: <script>alert('1111')</script>
转义后:<script>alert(\'1111\')<\/script>
反转义后:<script>alert('1111')</script>Process finished with exit code 0