Java转义工具类StringEscapeUtils的学习笔记

news/2024/10/18 2:27:53/

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>
转义后:&lt;strong&gt;加粗字符&lt;/strong&gt;
反转义后:<strong>加粗字符</strong>
-------------------------------------------------------------
转义/反转义xml字符串
原字符串: <xml>"xml字符串"</xml>
转义后:&lt;xml&gt;&quot;xml字符串&quot;&lt;/xml&gt;
反转义后:<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

http://www.ppmy.cn/news/589292.html

相关文章

diy nas配置推荐2020_NAS的DIY配置

主板:七彩虹(Colorful)i-AE50 V14 ITX主板(AMD APU E350) 链接:http://item.jd.com/498013.html 价格:559元 内存:金士顿(Kingston)DDR3 1333 4G 台式机内存 链接:http://item.jd.com/253363.html 价格:209元(杯具的价格,涨价很久了) 机箱:万由 存储机箱 热拔插机箱 NA…

CSV导出内容包含逗号

在项目中&#xff0c;需要导出查询结果集&#xff0c;但是每天记录的其中一项描述内容可以包含逗号&#xff0c;一开始的解决方案是直接把内容的逗号替换为点。很显然改动了用户体验&#xff0c;客户可不允许这样做&#xff0c;没得办法直接到网上找夏解决方案。 Example Year…

python读取CSV文件

具体的就不说了&#xff0c;说几个注意点&#xff1a; 1.传递给csv.reader或者DictReader的是一个打开的文件流&#xff1b; 2.异常“line contains NULL byte”可以对打开的文件作如下处理&#xff0c; csvfile open(filepath,"rb"); #打开一个csv文件 reader c…

金融数据分析 实验四 金融风险价值计算

实验原理 VaR的定义&#xff1a;在正常的市场条件下&#xff0c;给定置信水平和持有期&#xff0c;某种投资组合可能发生的最大损失值。 VaR模型不仅描述了损失的大小&#xff0c;还描述了发生损失的概率。 VaR的数学定义&#xff1a;给定置信水平 1 - α 和时间间隔 t ,如果一…

算法Day43 | 1049. 最后一块石头的重量 II,494. 目标和,474.一和零

Day43 1049. 最后一块石头的重量 II494. 目标和474.一和零 1049. 最后一块石头的重量 II 题目链接&#xff1a;1049. 最后一块石头的重量 II 石头相撞&#xff0c;得到最小重量 -> 分成重量近似的两堆 -> 得到结果 分成重量近似的两堆可以用 01背包来求得&#xff08;…

java操作csv文件(读、写)

今天在做项目的时候&#xff0c;发现使用POI无法解析以csv文件结尾的文件&#xff0c;虽然csv文件能用Excel打开&#xff0c;但是csv文件没有像Excel一样有规定的电子表格形式&#xff0c;故使用POI无法解析csv文件&#xff0c;在网上找了一下&#xff0c;发现java有提供javacs…

CSV文件格式

CSV &#xff08;逗号分隔值文件格式&#xff09; 编辑 逗号分隔值&#xff08;Comma-Separated Values&#xff0c; CSV&#xff0c;有时也称为 字符分隔值&#xff0c;因为分隔字符也可以不是逗号&#xff09;&#xff0c;其文件以纯文本形式存储表格数据&#xff08;数字…

家庭云及其它(中)

硬件 基本上0bug老师对自己的需求还是比较清楚的&#xff0c;所以没有选择NAS产品&#xff0c;也没有选择企业级服务器都是很正确的。当然选择APU算是一个小失误&#xff0c;这个方案更适合做HTPC&#xff0c;而不是家用服务器&#xff0c;N550不论从功耗还是发热上&#xff0…