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

StringhtmlEncode(String txt)
html Encode
if (null != txt) {
    txt = txt.replace("&", "&").replace("&", "&").replace(""", """)
            .replace("\"", "&quot;").replace("&amp;lt;", "&lt;").replace("<", "&lt;")
            .replace("&amp;gt;", "&gt;").replace(">", "&gt;").replace("&amp;nbsp;", "&nbsp;");
return txt;
StringhtmlEncoded(String text)
Substitutes conflicting characters in text with html masked expressions.
StringBuffer b;
int i, len;
char c;
len = text.length();
b = new StringBuffer(len * 3);
for (i = 0; i < len; i++) {
    c = text.charAt(i);
    switch (c) {
...
StringhtmlEncoder(String content)
html Encoder
content = content.replaceAll("\n", "<br>");
content = content.replaceAll("<", "&lt");
content = content.replaceAll(">", "&gt");
content = content.replaceAll(" ", "&nbsp;");
return content;
StringhtmlEncoding(String str)
html Encoding
StringBuffer bfu = new StringBuffer();
if (str != null) {
    String s = "&#";
    char[] cs = str.toCharArray();
    for (char c : cs) {
        int it = c;
        bfu.append(s).append(it).append(";");
return bfu.toString();
StringhtmlEntityEncode(String s)
Encodes given string with utf-8 encoding for html.
StringBuffer buf = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') {
        buf.append(c);
    } else {
        buf.append("&#" + (int) c + ";");
return buf.toString();
Stringhtmlize(CharSequence q)
Return a string which represents a CharSequence in HTML.
StringBuilder sb = new StringBuilder(q.length() * 2);
htmlize(q, sb);
return sb.toString();
Stringhtmlize(String str)
Surrounds string with html
return "<html>" + str + "</html>";
Stringhtmlize(String text)
htmlize
if (text == null)
    return null;
return text.replace(">", "&gt;").replace("<", "&lt;"); 
StringBufferhtmlize(StringBuffer sb)
ACME StringBuffer htmlizer.
StringBuffer out = new StringBuffer(sb.length() + 100);
out.insert(0, "<table border=\"1\"><tr><td>");
for (int i = 0; i < sb.length(); i++) {
    char c = sb.charAt(i);
    if (c == '\t') {
        out.append("</td><td>");
    } else if (c == '\n') {
        out.append("</td></tr>\n<tr><td>");
...
StringhtmlSpecialChars(String handleStr)
htmlspecialchars, Change HTML special char in String
return htmlSpecialChars(handleStr, true);