Java HTML Encode toHtmlText(String text)

Here you can find the source of toHtmlText(String text)

Description

to Html Text

License

Open Source License

Declaration

public static String toHtmlText(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String toHtmlText(String text) {
        StringBuffer htmlText = null;
        for (int i = 0; i < text.length(); i++) {
            String rep; // replacement string
            char ch = text.charAt(i);
            switch (ch) {
            case '<':
                rep = "&lt;"; // NOI18N
                break;
            case '>':
                rep = "&gt;"; // NOI18N
                break;
            case '\n':
                rep = "<br>"; // NOI18N
                break;
            default:
                rep = null;/*from   w w  w .j  a  v a 2  s  .  c  o  m*/
                break;
            }

            if (rep != null) {
                if (htmlText == null) {
                    // Expect 20% of text to be html tags text
                    htmlText = new StringBuffer(120 * text.length() / 100);
                    if (i > 0) {
                        htmlText.append(text.substring(0, i));
                    }
                }
                htmlText.append(rep);

            } else { // no replacement
                if (htmlText != null) {
                    htmlText.append(ch);
                }
            }
        }
        return (htmlText != null) ? htmlText.toString() : text;
    }
}

Related

  1. toHtmlString(String input)
  2. toHTMLString(String org)
  3. toHtmlString(String s)
  4. toHTMLString(String str)
  5. toHtmlText(String s)
  6. toHtmlTitle(final String title)
  7. toHtmlUrl(String url, String displayLabel)
  8. toHtmlValue(String s)
  9. toHtmlVerticalString(final String source)