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

StringtoHtml(String value, String defaultValue)
to Html
if (value == null)
    return defaultValue;
StringBuffer result = new StringBuffer(value.length());
char ch = ' ';
for (int i = 0; i < value.length(); i++) {
    switch (ch = value.charAt(i)) {
    case '<':
        result.append("&lt;");
...
StringtoHTML(String xhtml)
Unfortunately JEditorPane will only display HTML3.2, whereas to embed HTML in an xsd we must use XHTML so it will be valid XML.
int startPos, endPos;
while ((startPos = xhtml.indexOf("<?")) != -1 && (endPos = xhtml.indexOf("?>")) != -1) {
    xhtml = xhtml.substring(0, startPos) + xhtml.substring(endPos + 2);
while ((startPos = xhtml.indexOf("/>")) != -1) {
    xhtml = xhtml.substring(0, startPos) + xhtml.substring(startPos + 1);
return xhtml;
...
StringtoHtmlChars(String htmlReady)
Description of the Method
if (htmlReady != null) {
    htmlReady = replace(htmlReady, "\u00A1", "&iexcl;");
    htmlReady = replace(htmlReady, "\u00A2", "&cent;");
    htmlReady = replace(htmlReady, "\u00A3", "&pound;");
    htmlReady = replace(htmlReady, "\u00A4", "&curren;");
    htmlReady = replace(htmlReady, "\u00A5", "&yen;");
    htmlReady = replace(htmlReady, "\u00A6", "&brvbar;");
    htmlReady = replace(htmlReady, "\u00A7", "&sect;");
...
StringtoHTMLContentString(String s, boolean replaceSpaces, boolean isXhtml)
to HTML Content String
return toHTMLString(s, replaceSpaces, isXhtml);
StringtoHtmlCR(String text)
Convert the C line break sequence : "\n", "\r", "\r\n"
to HTML line break sequence.
return replaceCR(text, "<br/>");
StringtoHTMLEncode(String string)
to HTML Encode
if (string == null) {
    return null;
string = string.replaceAll("<", "&lt;");
string = string.replaceAll("\n", "<br />");
return string;
StringtoHTMLEscapedText(String s)
to HTML Escaped Text
return toXMLEscapedText(s).replaceAll("\n", "<br>\n");
StringtoHtmlInput(String str)
to Html Input
if (str == null)
    return "";
String html = new String(str);
html = html.replaceAll("<", "&lt;");
html = html.replaceAll(">", "&gt;");
return html;
StringtoHtmlLine(String input)
to Html Line
return ((input == null) ? "" : input + "<br/>");
StringtoHTMLString(String in)
to HTML String
if (in == null || in.length() <= 0) {
    return "";
StringBuffer out = new StringBuffer();
for (int i = 0; i < in.length(); i++) {
    char c = in.charAt(i);
    if (c == '\'')
        out.append("'");
...