Java HTML htmlText(String text)

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

Description

This method converts texts to be displayed on html-page.

License

Open Source License

Declaration

public static String htmlText(String text) 

Method Source Code

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

public class Main {
    /**/*  w ww  .  java  2 s  .co  m*/
    * This method converts texts to be displayed on html-page. Following conversion are done "<" =>
    * "&lt;" , ">" => "&gt;" and "&" => "&amp;", "\n" => "<br>
    * "
    */
    public static String htmlText(String text) {
        StringBuilder sb = new StringBuilder();
        char c;
        if (text == null)
            return "";
        for (int i = 0; i < text.length(); i++) {
            c = text.charAt(i);
            switch (c) {
            case '<':
                sb.append("&lt;");
                break;
            case '>':
                sb.append("&gt;");
                break;
            case '&':
                sb.append("&amp;");
                break;
            case '\"':
                sb.append("&quot;");
                break;
            case '\n':
                sb.append("<br>");
                break;
            default:
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

Related

  1. htmlNewline(String text)
  2. htmlSafe(String field)
  3. htmlSafe(String value)
  4. htmlSize(String str)
  5. htmlString(String str)
  6. HTMLtoRGB(String htmlColor)
  7. HTMLToTriplet(String color)
  8. htmlWrap(String text)
  9. htmlWrap(StringBuilder s)