Java – How to escape String


String in HTML should not contain special characters. If String contains special character, it should escape string using StringEscapeUtil class.
This class can escape special characters from String. Final string comes with HTML code.

Download apache commons api

import org.apache.commons.lang.StringEscapeUtils;

public class EscapeString {

    public static void main(String[] args) {

        String str="<HTML> tag's value  "into" html & tag";

        String escapedString=StringEscapeUtils.escapeHtml(str);



&lt;HTML&gt; tag’s value &quot;into&quot; html text &amp; tag



Leave a Reply