Java HTML Encode textToHTML(String text)

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

Description

text To HTML

License

Open Source License

Declaration

public static String textToHTML(String text) 

Method Source Code

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

public class Main {
    public static String textToHTML(String text) {
        if (text == null) {
            return null;
        }//w  w w . j av  a 2  s .co  m
        int length = text.length();
        boolean prevSlashR = false;
        String out = "";
        for (int i = 0; i < length; i++) {
            char ch = text.charAt(i);
            switch (ch) {
            case '\r':
                if (prevSlashR) {
                    out += "<br>";
                }
                prevSlashR = true;
                break;
            case '\n':
                prevSlashR = false;
                out += "<br>";
                break;
            case '"':
                if (prevSlashR) {
                    out += "<br>";
                    prevSlashR = false;
                }
                out += "&quot;";
                break;
            case '<':
                if (prevSlashR) {
                    out += "<br>";
                    prevSlashR = false;
                }
                out += "&lt;";
                break;
            case '>':
                if (prevSlashR) {
                    out += "<br>";
                    prevSlashR = false;
                }
                out += "&gt;";
                break;
            case '&':
                if (prevSlashR) {
                    out += "<br>";
                    prevSlashR = false;
                }
                out += "&amp;";
                break;
            default:
                if (prevSlashR) {
                    out += "<br>";
                    prevSlashR = false;
                }
                out += ch;
                break;
            }
        }
        return (out);
    }
}

Related

  1. textToHtml(final String s)
  2. textToHtml(String html)
  3. textToHTML(String message)
  4. textToHtml(String s)
  5. textToHtml(String text)
  6. textToHtml(String text)
  7. textToHTML(String text)
  8. textToHTML(String text)
  9. toHtml(String message)