Java Utililty Methods HTML Encode

List of utility methods to do HTML Encode

Description

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

Method

StringtoHtml(String s)
Returns the specified string converted to a format suitable for HTML.
if (s == null) {
    return null;
for (String[] substitution : HTML_SUBSTITUTIONS) {
    if (s.contains(substitution[0])) {
        s = s.replaceAll(substitution[0], substitution[1]);
return s;
StringtoHtml(String str)
to Html
if (str == null)
    return "";
StringBuffer sb = new StringBuffer();
int len = str.length();
for (int i = 0; i < len; i++) {
    char c = str.charAt(i);
    switch (c) {
    case ' ':
...
StringtoHtml(String str)
to Html
if (str == null)
    return "";
str = str.replaceAll("&", "&amp;");
str = str.replaceAll("<", "&lt;");
str = str.replaceAll(">", "&gt;");
str = str.replaceAll("\"", "&quot;");
str = str.replaceAll("\r\n", "\n");
str = str.replaceAll("\n", "<br/>");
...
StringtoHtml(String str)
to Html
return str.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("'", "&apos;")
        .replace("\"", "&quot;").replace("(", "&#40;").replace(")", "&#41;").replace("[", "&#91;")
        .replace("]", "&#93;").replace("{", "&#123;").replace("|", "&#124;").replace("}", "&#125;")
        .replace("/", "&#47;").replace(",", "&#44;").replace("\\", "&#92;");
StringtoHtml(String str)
to Html
if (str == null)
    return "";
String html = new String(str);
html = toHtmlInput(html);
html = html.replaceAll("\r\n", "\n");
html = html.replaceAll("\n", "<br>\n");
html = html.replaceAll("\t", "    ");
html = html.replaceAll("  ", " &nbsp;");
...
StringtoHTML(String string)
to HTML
if (string != null) {
    string = string.replace("&", "&amp;");
    string = string.replace("\"", "&quot;");
    string = string.replace("'", "&apos;");
    string = string.replace("<", "&lt;");
    string = string.replace(">", "&gt;");
return string;
...
StringtoHTML(String text)
Converts a multiline string to an HTML-string that can be displayed in a label.
StringBuilder buf = new StringBuilder("<HTML>");
if (text != null) {
    buf.append(text.replace("\n", "<BR>"));
buf.append("</HTML>");
return buf.toString();
StringtoHtml(String text)
to Html
text = text.replaceAll("\n", "<br/>");
text = text.replaceAll("  ", "&nbsp;&nbsp;");
return text;
StringtoHtml(String trace)
to Html
return trace.replaceAll("&", "&quot;").replaceAll("<", "&lt;").replaceAll(">", "&gt;")
        .replaceAll("\"", "&quot;").replaceAll("\t", "&nbsp; &nbsp; ");
StringtoHtml(String txt)
to Html
return txt.replaceAll("\n", "<br>");