Java tool class is Apache's StringEscapeUtils escape tool class


This number is mainly the sharing of common key technology points and common tool classes of Java; And technology sharing of integration frameworks such as springboot+springcloud+Mybatisplus+druid+mysql+redis+swagger+maven+docker; Technology sharing of big data processing frameworks such as datax, kafka and flink. The article will be updated constantly. Code friends are welcome to pay attention, like collection and forwarding!

I hope all code friends can click to pay attention and make 1000 powder. Some video tutorials will be recorded later, combining graphics and video, such as Book Introduction website system, rush purchase system, big data middle desk system, etc. Technology is the favorite of program apes. Code friends rush

If the coder thinks the code is too long, he can quickly scan it from beginning to end and understand it. If you find it useful, you can forward it for collection in case of need.


Apache's StringEscapeUtils escape tool class is mainly used to escape html, json, xml, js, etc. There are not many specific usage scenarios in the project, so few people may know about them.

Stringescape utils escape example

 import org.apache.commons.text.StringEscapeUtils;
 import org.junit.Test;
 public class StringEscapeUtilsTest {
     public void test() {
         // Escape html script and anti escape html script
         String inputText = "<input type=\"button\" value=\"Point me\"/>";
         String s1 = StringEscapeUtils.escapeHtml4(inputText);
         String s2 = StringEscapeUtils.unescapeHtml4(s1);
         // Escape js script and anti escape js script
         String s3 = StringEscapeUtils.escapeEcmaScript("<script>alert('Point me')<script>");
         String s4 = StringEscapeUtils.unescapeEcmaScript(s3);
         // Escaping strings to unicode encoding and from Unicode encoding to strings
         String s5 = StringEscapeUtils.escapeJava("abc Don't order me");
         String s6 = StringEscapeUtils.unescapeJava(s5);
         // Escape XML and anti escape XML
         String s7 = StringEscapeUtils.escapeXml11("<name>Zhang San</name>");
         String s8 = StringEscapeUtils.unescapeXml(s7);

Other methods:

Tool source code:

You can directly import the jar package of the tool class


Tool classes with the same functions as those used in this article:

  • org.apache.commons.lang.StringEscapeUtils is the same as the org.apache.commons.text.StringEscapeUtils tool class, but Apache annotates it as outdated. It also has a powerful method to escape sql [escapeql], but it is recommended to use a new package org.apache.commons.text.StringEscapeUtils;


  • org.springframework.web.util.HtmlUtils, a tool class of spring, mainly for html. You can learn about it;


  • groovy.json.StringEscapeUtils has the same functions as org.apache.commons.text.StringEscapeUtils. Generally, this package is not used for projects, but you can learn about it; If you can use groovy scripts, you should use more. Groovy XXX has many tool classes, but I haven't used them. If you are interested, you can explore them.


I have been coding for more than ten years and have accumulated some tool classes in the project. Many tool classes are used in every project and are very practical. Most of them are encapsulated by me. Some tool classes are encapsulated by colleagues. Some tool classes don't remember whether they are ctrl+c or encapsulated by themselves. Now I will summarize most of the tool classes in the project and share them with you when I am free. If the code involved in the article has infringement, please notify me for handling.

The plan is to sort out the tools first. As the saying goes, if you want to do well, you must first sharpen the tools. In the project, whether it is an ordinary single project, a multi module maven project or a distributed microservice, some functional modules can be reused, and the tool module is one of them.

Tags: Java

Posted on Tue, 16 Nov 2021 18:05:21 -0500 by dickie_doodle