Java HTML Encode toHtml(String value, String defaultValue)

Here you can find the source of toHtml(String value, String defaultValue)

Description

to Html

License

LGPL

Declaration

public static String toHtml(String value, String defaultValue) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static String toHtml(String value, String defaultValue) {
        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;");
                break;
            case '>':
                result.append("&gt;");
                break;
            case '&':
                result.append("&amp;");
                break;
            case '\'':
                result.append("&#39;");
                break;
            case '"':
                result.append("&quot;");
                break;
            default:
                result.append(ch);/*w  ww .  j  a v  a2 s .  c  om*/
            }
        }
        return result.toString();
    }

    public static String toHtml(String value) {
        return toHtml(value, "");
    }
}

Related

  1. toHTML(String string)
  2. toHTML(String text)
  3. toHtml(String text)
  4. toHtml(String trace)
  5. toHtml(String txt)
  6. toHTML(String xhtml)
  7. toHtmlChars(String htmlReady)
  8. toHTMLContentString(String s, boolean replaceSpaces, boolean isXhtml)
  9. toHtmlCR(String text)