• StringEscapeUtils的使用


    使用commons-lang.jar

    import org.apache.commons.lang.StringEscapeUtils;
    
    public class T {
        public static void main(String[] args) {
            String str = "thi is a test 这是一个测试";
            String xml = "<?xml version="1.0" encoding="UTF-8"?>
    " +
                    "<persons>
    " +
                    "   <person id="23">
    " +
                    "         <name>张   三</name>
    " +
                    "         <age>26</age>
    " +
                    "  </person>
    " +
                    "  <person id="22">
    " +
                    "        <name>李四</name>
    " +
                    "        <age>25</age>
    " +
                    " </person>
    " +
                    "</persons>";
    
            System.out.println("用escapeJava方法转义之后的字符串为:"+ StringEscapeUtils.escapeJava(str));
    
            System.out.println("用unescapeJava方法反转义之后的字符串为:"+StringEscapeUtils.unescapeJava(StringEscapeUtils.escapeJava(str)));
    
    
    
            System.out.println("用escapeHtml方法转义之后的字符串为:"+StringEscapeUtils.escapeHtml(str));
    
            System.out.println("用unescapeHtml方法反转义之后的字符串为:"+StringEscapeUtils.unescapeHtml(StringEscapeUtils.escapeHtml(str)));
    
    
            System.out.println("用escapeXml方法转义之后的字符串为:"+StringEscapeUtils.escapeXml(xml));
    
            System.out.println("用unescapeXml方法反转义之后的字符串为:"+StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeXml(xml)));
    
    
    
            System.out.println("用escapeJavaScript方法转义之后的字符串为:"+StringEscapeUtils.escapeJavaScript(str));
    
            System.out.println("用unescapeJavaScript方法反转义之后的字符串为:"+StringEscapeUtils.unescapeJavaScript(StringEscapeUtils.unescapeJavaScript(str)));
        }
    }

    运行结果:

    用escapeJava方法转义之后的字符串为:thi is a test u8FD9u662Fu4E00u4E2Au6D4Bu8BD5
    用unescapeJava方法反转义之后的字符串为:thi is a test 这是一个测试
    用escapeHtml方法转义之后的字符串为:thi is a test &#36825;&#26159;&#19968;&#20010;&#27979;&#35797;
    用unescapeHtml方法反转义之后的字符串为:thi is a test 这是一个测试
    用escapeXml方法转义之后的字符串为:&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;persons&gt;
       &lt;person id=&quot;23&quot;&gt;
             &lt;name&gt;&#24352;   &#19977;&lt;/name&gt;
             &lt;age&gt;26&lt;/age&gt;
      &lt;/person&gt;
      &lt;person id=&quot;22&quot;&gt;
            &lt;name&gt;&#26446;&#22235;&lt;/name&gt;
            &lt;age&gt;25&lt;/age&gt;
     &lt;/person&gt;
    &lt;/persons&gt;
    用unescapeXml方法反转义之后的字符串为:<?xml version="1.0" encoding="UTF-8"?>
    <persons>
       <person id="23">
             <name>张   三</name>
             <age>26</age>
      </person>
      <person id="22">
            <name>李四</name>
            <age>25</age>
     </person>
    </persons>
    用escapeJavaScript方法转义之后的字符串为:thi is a test u8FD9u662Fu4E00u4E2Au6D4Bu8BD5
    用unescapeJavaScript方法反转义之后的字符串为:thi is a test 这是一个测试
  • 相关阅读:
    UWP 常用文件夹
    UWP 判断Windows10系统版本
    UWP 图片缩放
    Windows 10「设置」应用完整MS-Settings快捷方式汇总
    UWP 用Thumb 控件仿制一个可拖动悬浮 Button
    【mp3】洗脑循环了!龙珠超 自在极意功 【究极の圣戦】串田アキラ 背景纯音乐
    工作三年后的总结
    css3 移动端 开关效果
    js 移动端上拉加载下一页通用方案
    【我的上传番剧/电影】收藏夹
  • 原文地址:https://www.cnblogs.com/mouseIT/p/4929725.html
Copyright © 2020-2023  润新知