Unsurprisingly, today is also a day for articles, with some interesting tool classes worth exploring. I wrote this a long time ago, but it’s a little out of date, but I hope you have time to explore some of the tool classes, there are some very clever design.

The Java.com mons.lang3 package has many handy utility classes, such as StringUtils for strings, DateUtils for dates, and so on. StringEscapeUtils is one of them.

StringEscapeUtils is a utility class added in version 2.0 of Java.com mons.lang3. In version 3.6, StringEscapeUtils is marked as @deprecated, indicating that it will become obsolete in later versions. The StringEscapeUtils class was later moved to the Java.com mons.text package.

1. Functional purposes

The main function of StringEscapeUtils is to escape and reverse escape Java, Java Script, Html and XML.

  • EscapeJava (String Input)/unescapeJava(String unionCodeString) Converts the input String to Unicode encoding/Converts the Unicode String to UTF-8 format
  • EscapeHtml4 (String Input)/unescapeHtml4(String input) Escape/reverse escape HTML scripts
  • EscapeEcmaScript (String Input)/unescapeEcmaScript(String Input) Escapes/unescapes JS scripts
  • EscapeXml (String Input)/unescapeXml(String input) Escapes or escapes XML scripts

In addition to a few of the more common methods listed, EscapeJson (String Input)/unescapeJson(String input), escapeCsv(String Input)/unescapeCsv(String input), etc. To get an idea, look at the following implementation example.

2. Execution examples

Introduce the Java.com mons.text package into the project (the version number can be selected from the official website) :

Gradle:

// https://mvnrepository.com/artifact/org.apache.commons/commons-text compile group: 'org.apache.commons', name: 'Commons - text, version:' 1.3 'Copy the code

Maven:

<! -- https://mvnrepository.com/artifact/org.apache.commons/commons-text --> <dependency> Mons < groupId > org.apache.com < / groupId > < artifactId > Commons - text < / artifactId > < version > 1.3 < / version > < / dependency >Copy the code
import org.apache.commons.text.StringEscapeUtils;
import org.junit.Test;

/ * * *@author liuqian
 * @date2018/4/3 16:27 * /
public class EscapeTest {

  @Test
  public void escapeTest(a) {
    System.out.println("Escape/unescape Java strings");
    String javaString = "This is a Java string.";
    System.out.println(StringEscapeUtils.escapeJava(javaString));
    System.out.println(StringEscapeUtils.unescapeJava(StringEscapeUtils.escapeJava(javaString)));
    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
    System.out.println("Escape/reverse escape Json string");
    String jsonString = "{\"keyword\": \" This is Json string \"}";
    System.out.println(StringEscapeUtils.escapeJson(jsonString));
    System.out.println(StringEscapeUtils.unescapeJson(StringEscapeUtils.escapeJson(jsonString)));
    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
    // In addition to html4, html3 and other formats
    System.out.println("Escape/reverse escape Html strings");
    String htmlString = " bold ";
    System.out.println(StringEscapeUtils.escapeHtml4(htmlString));
    System.out.println(StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(htmlString)));
    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
    // In addition to xml10, there are formats such as XML11
    System.out.println("Escape/reverse escape XML string");
    String xmlString = "< XML >\" XML string \"
      ";
    System.out.println(StringEscapeUtils.escapeXml10(xmlString));
    System.out.println(StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeXml10(xmlString)));
    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
    System.out.println("Escape/reverse escape CSV string");
    String csvString = "1997,Ford,E350,\"Super, luxurious truck\"";
    System.out.println(StringEscapeUtils.escapeCsv(csvString));
    System.out.println(StringEscapeUtils.unescapeCsv(StringEscapeUtils.escapeCsv(csvString)));
    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
    System.out.println("Escape/unescape Java Script strings");
    String jsString = "<script>alert('1111')</script>"; System.out.println(StringEscapeUtils.escapeEcmaScript(jsString)); System.out.println(StringEscapeUtils.unescapeEcmaScript(StringEscapeUtils.escapeEcmaScript(jsString))); }}Copy the code

The results are as follows:

Escape/unescapes Java string \ u8FD9 \ u662FJava \ u5B57 \ u7B26 \ u4E32 this is Java string -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Escape/Unescape Json String {\"keyword ": \"\u8FD9\u662FJson\u5B57\u7B26\u4E32\"} {\"keyword ": \ "this is a Json string \}" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - escape/unescapes Html string & lt; strong&gt; Bold character &lt; /strong&gt; < strong > < / strong > bold character -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - escape/unescapes XML string & lt; xml&gt; &quot; XML string "; &lt; /xml&gt; XML < > "XML string" < / XML > -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - escape/unescapes CSV string "1997, Ford, on the," "Super, luxurious truck""" 1997,Ford,E350,"Super, Luxurious truck "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - escape/unescapes Java Script string <script>alert(\'1111\')<\/script> <script>alert('1111')</script>Copy the code