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

StringHTMLBREncode(String str)
HTMLBR Encode
return str.replaceAll("\r\n", "\r\n<br class=\"br\"/>\r\n");
voidhtmlEncode(final String source, StringBuffer target)
Html encode, with StringBuffer as target (faster)
if (source == null || source.length() == 0)
    return;
int length = source.length();
char[] charArray = new char[length];
source.getChars(0, length, charArray, 0);
int startIndex = 0;
int currentIndex = 0;
while (currentIndex < length) {
...
StringhtmlEncode(Object input)
html Encode
if (input == null) {
    return "";
String strInput = input.toString().trim();
StringBuilder output = new StringBuilder();
int len = strInput.length();
char ch;
for (int i = 0; i < len; i++) {
...
StringhtmlEncode(String html)
Replaces all HTML-sensitive characters with their entity equivalents
if (html == null) {
    return null;
StringBuffer buf = new StringBuffer((int) (html.length() * 1.1));
int stringLength = html.length();
for (int i = 0; i < stringLength; ++i) {
    char c = html.charAt(i);
    switch (c) {
...
StringhtmlEncode(String html)
Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
if (hasText(html)) {
    String result = html.replace("&", HTML_AMPERSAND);
    result = result.replace("<", HTML_GREATER_THAN);
    result = result.replace(">", HTML_LESS_THAN);
    result = result.replace("'", HTML_DASH_POINT);
    return result;
return null;
...
StringhtmlEncode(String input)
html Encode
return input.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("  ", "&nbsp;&nbsp;")
        .replace(LINE_BREAK, "<br>" + LINE_BREAK);
StringhtmlEncode(String input)
HTML-encode a string.
String output = input.replaceAll("&", "&amp;");
output = output.replaceAll("<", "&lt;");
output = output.replaceAll(">", "&gt;");
output = output.replaceAll("\"", "&quot;");
output = output.replaceAll("'", "&#039;");
return output;
StringhtmlEncode(String s)
html Encode
StringBuffer out = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c > 127 || c == '"' || c == '<' || c == '>') {
        out.append("&#" + (int) c + ";");
    } else {
        out.append(c);
return out.toString();
StringhtmlEncode(String s)
html Encode
StringBuffer sb = new StringBuffer();
int len = s.length();
for (int counter = 0; counter < len; counter++) {
    char c = s.charAt(counter);
    switch (c) {
    case '<':
        sb.append("&lt;");
        break;
...
StringhtmlEncode(String s)
Returns HTML encoded version of the specified String s.
StringBuffer encodedString = new StringBuffer("");
char[] chars = s.toCharArray();
for (char c : chars) {
    if (c == '<') {
        encodedString.append("&lt;");
    } else if (c == '>') {
        encodedString.append("&gt;");
    } else {
...