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

StringhtmlSpecialChars(String html)
html Special Chars
StringBuilder builder = new StringBuilder(html);
for (int index = 0; index < builder.length(); index++) {
    String toInsert;
    switch (builder.charAt(index)) {
    case '<':
        toInsert = "&lt;";
        break;
    case '>':
...
Stringhtmlspecialchars(String input)
htmlspecialchars
if (input == null)
    return null;
String retval = input.replace("&", "&amp;");
retval = retval.replace("\"", "&quot;");
retval = retval.replace("<", "&lt;");
retval = retval.replace(">", "&gt;");
retval = retval.replace(">", "&gt;");
retval = retval.replace("=", "&#61;");
...
Stringhtmlspecialchars(String s)
translate some HTML characters (similar to same PHP function) The translations performed are: '&' (ampersand) becomes '&' NOT YET IMPLEMENTED '"' (double quote) becomes '"' NOT YET IMPLEMENTED ''' (single quote) becomes ''' NOT YET IMPLEMENTED '<' (less than) becomes '<' '>' (greater than) becomes '>'
s = s.replaceAll("<", "&lt;");
s = s.replaceAll(">", "&gt;");
return s;
Stringhtmlspecialchars(String str)
htmlspecialchars
str = str.replaceAll("&", "&amp;");
str = str.replaceAll("<", "&lt;");
str = str.replaceAll(">", "&gt;");
str = str.replaceAll("\"", "&quot;");
return str;
StringtextToHtml(final String s)
Converts a text message into an HTML message, replacing all newlines with <br> tags and escaping the '<' and '>' characters;
final StringBuilder sb = new StringBuilder();
for (final char c : s.toCharArray()) {
    switch (c) {
    case '\n':
        sb.append("<br/>");
        break;
    case '<':
        sb.append("&lt;");
...
StringtextToHtml(String html)
text To Html
if (html != null) {
    html = html.replace("\u00e5", "&aring;");
    html = html.replace("\u00e4", "&auml;");
    html = html.replace("\u00f6", "&ouml;");
    html = html.replace("\u00c5", "&Aring;");
    html = html.replace("\u00c4", "&Auml;");
    html = html.replace("\u00d6", "&Ouml;");
return html;
StringtextToHTML(String message)
Scans a text, generating HTML that respects leading/consecutive spaces and newlines, while escaping actual HTML constructs.
StringBuffer html = new StringBuffer();
boolean collectSpaces = true;
for (int i = 0; i < message.length(); i++) {
    String bit;
    char c = message.charAt(i);
    switch (c) {
    case '\r':
        continue;
...
StringtextToHtml(String s)
wrap contents as an html document
return stringToHTMLString(s);
StringtextToHtml(String text)
Converts plaintext to HTML, preserving whitespace semantics as much as possible.
return text.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;")
        .replaceAll("\\n", "<br />").replaceAll("\\t", "    ").replaceAll(" ", "&nbsp;")
        .replaceAll("&nbsp;(?!&nbsp;)", " ").replaceAll(" $", "&nbsp;").replaceAll("^ ", "&nbsp;");
StringtextToHTML(String text)
text To HTML
if (text == null) {
    return null;
int length = text.length();
boolean prevSlashR = false;
String out = "";
for (int i = 0; i < length; i++) {
    char ch = text.charAt(i);
...