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 str)
html Encode
if (str == null) {
    return "";
String tmp = str;
tmp = replace(tmp, "&", "&", false);
tmp = replace(tmp, "<", "&lt;", false);
tmp = replace(tmp, "\"", "&quot;", false);
tmp = replace(tmp, ">", "&gt;", false);
...
StringhtmlEncode(String str)
Replaces the chars '>', '<', and '&' with the sequences '>', '<', and '&'.
StringBuilder result = null;
for (int i = 0, max = str.length(), delta = 0; i < max; i++) {
    char c = str.charAt(i);
    String replacement = null;
    if (c == '&') {
        replacement = "&amp;";
    } else if (c == '<') {
        replacement = "&lt;";
...
StringhtmlEncode(String string)
Escapes all characters that may cause a problem if a string is used directly in HTML.
StringBuilder sb = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
    char c = string.charAt(i);
    if (c > 127 || c == '\"' || c == '\'' || c == '&' || c == '<' || c == '>' || c == '#' || c == ';')
        sb.append("&#").append((int) c).append(';');
    else
        sb.append(c);
return sb.toString();
StringHTMLEncode(String string)
Encode a string to be displayed in HTML

If fullHTML encode, then all unicode characters above 7 bits are converted into HTML entitites

return HTMLEncode(string, false);
StringhtmlEncode(String string)
html Encode
String encoded = replaceString(string, '&', "&amp;");
encoded = replaceString(encoded, '<', "&lt;");
return replaceString(encoded, '>', "&gt;");
StringHtmlEncode(String strInput)
Html Encode
String input = strInput;
input = nullToEmpty(input);
if ("".equals(input)) {
    return "&nbsp;";
} else {
    input = Replace(input, "\"", "&quot;");
    input = Replace(input, "<", "&lt;");
    input = Replace(input, ">", "&gt;");
...
StringhtmlEncode(String strSrc)
html Encode
if (strSrc == null)
    return "";
char[] arr_cSrc = strSrc.toCharArray();
StringBuffer buf = new StringBuffer(arr_cSrc.length);
char ch;
for (int i = 0; i < arr_cSrc.length; i++) {
    ch = arr_cSrc[i];
    if (ch == '<')
...
StringhtmlEncode(String strSrc)
html Encode
if (strSrc == null) {
    return "";
char[] arrCsrc = strSrc.toCharArray();
StringBuilder buf = new StringBuilder(arrCsrc.length);
char ch;
for (char anArrCSrc : arrCsrc) {
    ch = anArrCSrc;
...
StringhtmlEncode(String text)
html Encode
if (text == null || text.length() == 0) {
    return text;
int length = text.length();
StringBuffer ret = new StringBuffer(length * 12 / 10);
boolean isEncodeSpace = true;
int last = 0;
for (int i = 0; i < length; i++) {
...
StringhtmlEncode(String txt)
Encodes the given text to HTML save form.
if (txt == null) {
    return "";
return txt.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;")
        .replace("'", "&#39;").replace("\r\n", "<br/>").replace("\n", "<br/>");