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

StringtoHtmlString(String input)
to Html String
if ((input == null) || (input.trim().length() == 0)) {
    return input;
StringBuffer result = new StringBuffer();
char tmp;
char lstchar = ' ';
for (int i = 0; i < input.length(); i++) {
    tmp = input.charAt(i);
...
StringtoHTMLString(String org)
to HTML String
StringBuffer result = new StringBuffer(org.length());
char[] chars = org.toCharArray();
for (int i = 0; i < chars.length; i++) {
    if (chars[i] == '\"')
        result.append("&quot;");
    else if (chars[i] == '<')
        result.append("&lt;");
    else if (chars[i] == '>')
...
StringtoHtmlString(String s)
to Html String
String toReturn = null;
if (s == null) {
    return null;
if (s.contains("!DOCTYPE HTML")) {
    s = replace(s, "!DOCTYPE HTML", "");
    return s;
toReturn = s;
if (toReturn.contains("<")) {
    if (toReturn.contains("</a>") || toReturn.contains("</A>")) {
        return toReturn;
    toReturn = replace(toReturn, "<B>", BOLD_START);
    toReturn = replace(toReturn, "<b>", BOLD_START);
    toReturn = replace(toReturn, "</B>", BOLD_END);
    toReturn = replace(toReturn, "</b>", BOLD_END);
    toReturn = replace(toReturn, "<BR>", NEW_LINE);
    toReturn = replace(toReturn, "<br>", NEW_LINE);
    toReturn = replace(toReturn, "<", "&lt;");
    toReturn = replace(toReturn, ">", "&gt;");
    toReturn = replace(toReturn, BOLD_START, "<B>");
    toReturn = replace(toReturn, BOLD_END, "</B>");
    toReturn = replace(toReturn, NEW_LINE, "<br>");
toReturn = replace(toReturn, "\r\n", "<br>");
toReturn = replace(toReturn, "\r", "<br>");
toReturn = replace(toReturn, "\n", "<br>");
toReturn = replace(toReturn, " ", "&nbsp;");
toReturn = replace(toReturn, "\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
return toReturn;
StringtoHTMLString(String str)
Converts the given unicode string to an html string where special characters are converted to &#xxx; sequences (xxx is the unicode value of the character)
if (str == null)
    return null;
StringBuffer sb = new StringBuffer();
int len = str.length();
for (int i = 0; i < len; i++) {
    char c = str.charAt(i);
    int code = c;
    if ((code >= 32 && code <= 126)) {
...
StringtoHtmlText(String s)
Description of the Method
s = replace(s, "<br>\r\n", "<br>");
s = replace(s, "\r\n", "<br>");
return s;
StringtoHtmlText(String text)
to Html Text
StringBuffer htmlText = null;
for (int i = 0; i < text.length(); i++) {
    String rep; 
    char ch = text.charAt(i);
    switch (ch) {
    case '<':
        rep = "&lt;"; 
        break;
...
StringtoHtmlTitle(final String title)
to Html Title
return "<title>" + title + "</title>";
StringtoHtmlUrl(String url, String displayLabel)
to Html Url
return String.format("<a href='%s'>%s</a>", url, displayLabel);
StringtoHtmlValue(String s)
Description of the Method
if (s != null) {
    String htmlReady = s.trim();
    htmlReady = replace(htmlReady, "&", "&amp;");
    htmlReady = replace(htmlReady, "\"", "&quot;");
    htmlReady = replace(htmlReady, "<", "&lt;");
    htmlReady = replace(htmlReady, ">", "&gt;");
    htmlReady = replace(htmlReady, "\r\n", "<br>");
    htmlReady = replace(htmlReady, "\n\r", "<br>");
...
StringtoHtmlVerticalString(final String source)
to Html Vertical String
if ((source == null) || (source.trim().length() == 0)) {
    return source;
final String tmp = source.trim();
final int len = tmp.length();
final StringBuffer result = new StringBuffer();
for (int i = 0; i < (len - 1); i++) {
    result.append(tmp.charAt(i)).append("<br>");
...