zoukankan      html  css  js  c++  java
  • 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 这是一个测试
  • 相关阅读:
    php cookie名不能使用点号(句号)
    jquery:iframe里面的元素怎样触发父窗口元素的事件?
    __destruct与register_shutdown_function执行的先后顺序问题
    curl: (60) SSL certificate problem: unable to get local issuer certificate 错误
    js与as3交互的问题
    启动smaba后nginx 11 resource temporarily unavailable[转载]
    PHP错误: Exception thrown without a stack frame in Unknown on line 0[转载]
    Discuz x2.5的注册后返回第三方应用
    laravel-admin select关联
    laravel-admin 自动生成模块
  • 原文地址:https://www.cnblogs.com/mouseIT/p/4929725.html
Copyright © 2011-2022 走看看