Java Unescape HTML to String Example

Java examples to unescape the characters in a String. It unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes.

StringEscapeUtils.unescapeHtml4() [Apache Commons Text]

  • This method takes escaped string as parameter. It can be null.
  • It supports all known HTML 4.0 entities.

To use StringEscapeUtils, import commons-text dependency.

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-text</artifactId>
	<version>1.4</version>
</dependency>

Now use StringEscapeUtils.unescapeHtml4() method.

import org.apache.commons.text.StringEscapeUtils;

public class HTMLEscapeExample 
{
	public static void main(String[] args) 
	{
		String escapedString = "&lt;java&gt;public static void main(String[] args) { ... }&lt;/java&gt;";
		
		String unEscapedHTML = StringEscapeUtils.unescapeHtml4(escapedString);
		
		System.out.println(unEscapedHTML);
	}
}

//Output:

<java>public static void main(String[] args) { ... }</java>

Happy Learning !!

References:

HTML 4.01 Character References
StringEscapeUtils.escapeHtml4()

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.