Java Utililty Methods HTML Escape

List of utility methods to do HTML Escape

Description

The list of methods to do HTML Escape are organized into topic(s).

Method

StringhtmlEscape(final String text)
Escapes the text into proper HTML, converting characters into character entities when required.
return text.replaceAll("&", "&") 
        .replaceAll("<", "&lt;").replaceAll(">", "&gt;");
StringhtmlEscape(Object obj)
html Escape
if (obj == null) {
    return "";
} else {
    return obj.toString().replaceAll("&", "&amp;").replaceAll(">", "&gt;").replaceAll("<", "&lt;")
            .replaceAll("\"", "&quot;");
StringhtmlEscape(String html)
html Escape
String htmlEscaped = "";
for (int i = 0; i < html.length(); i++) {
    char c = html.charAt(i);
    if (c == '\n') {
        htmlEscaped += "<br>";
    } else if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') {
        htmlEscaped += "&#" + ((int) c) + ';';
    } else {
...
Stringhtmlescape(String html)
htmlescape
return html.replace("&", "&amp;").replace("\"", "&quot;").replace("'", "&#039;").replace("<", "&lt;")
        .replace(">", "&gt;");
StringhtmlEscape(String input)
Convert &, ", < and > to their respective HTML escape sequences.
if (input == null)
    return "";
String res = input;
res = res.replaceAll("&", "&amp;");
res = res.replaceAll("\"", "&quot;");
res = res.replaceAll("<", "&lt;");
res = res.replaceAll(">", "&gt;");
return res;
...
StringhtmlEscape(String input)
html Escape
StringBuilder sb = new StringBuilder(input.length());
for (char c : input.toCharArray()) {
    if (replacements.containsKey(c)) {
        sb.append(replacements.get(c));
    } else {
        sb.append(c);
return sb.toString();
StringhtmlEscape(String input)
Escape input string suitable for HTML output.
String ret = input.replaceAll("&", "&amp;");
ret = ret.replaceAll("<", "&lt;");
ret = ret.replaceAll(">", "&gt;");
return ret;
StringhtmlEscape(String input)
Escape the HTML specific characters (like <) but keeps the UTF-8 content (for characters like é).
if (input == null) {
    return null;
String result = input.replace("&", "&amp;");
result = result.replace("<", "&lt;");
result = result.replace(">", "&gt;");
result = result.replace("\"", "&quot;");
return result;
...
StringhtmlEscape(String nonHTMLsrc)
This method will take the input and escape characters that have an HTML entity representation.
if (nonHTMLsrc == null)
    return null;
StringBuffer res = new StringBuffer();
int l = nonHTMLsrc.length();
int idx;
char c;
for (int i = 0; i < l; i++) {
    c = nonHTMLsrc.charAt(i);
...
StringhtmlEscape(String s)
Bare-bones version of getDisplayableHtmlString() above.
if (s == null)
    return null;
int len = s.length();
StringBuffer sb = new StringBuffer(len);
for (int i = 0; i < len; i++) {
    char c = s.charAt(i);
    switch (c) {
    case '<':
...