Java HTML Unescape unescapeHtml(String s)

Here you can find the source of unescapeHtml(String s)

Description

Reverses the escaping done by escapeForHtml.

License

Open Source License

Declaration

public static String unescapeHtml(String s) 

Method Source Code

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

public class Main {
    /**//from ww  w . j av a2s.c  om
     * Reverses the escaping done by escapeForHtml.
     * This method is only meant to cope with HTML from escapeForHtml, not general HTML using arbitrary entities.
     * Unknown entities will be preserved.
     */
    public static String unescapeHtml(String s) {
        final int sLength = s.length();
        final StringBuilder result = new StringBuilder(sLength);
        for (int i = 0; i < sLength; ++i) {
            final char ch = s.charAt(i);
            if (ch == '&') {
                if (s.startsWith("&amp;", i)) {
                    result.append('&');
                    i += 4;
                } else if (s.startsWith("&quot;", i)) {
                    result.append('"');
                    i += 5;
                } else if (s.startsWith("&gt;", i)) {
                    result.append('>');
                    i += 3;
                } else if (s.startsWith("&lt;", i)) {
                    result.append('<');
                    i += 3;
                } else {
                    // There are a lot of entities we don't know. Leave them unmolested.
                    result.append(ch);
                }
            } else {
                result.append(ch);
            }
        }
        return result.toString();
    }
}

Related

  1. unescapeHtml(final String input)
  2. unescapeHTML(String comment)
  3. unescapeHTML(String html)
  4. unescapeHtml(String s)
  5. unescapeHTML(String s)
  6. unescapeHTML(String s)
  7. unescapeHTML(String s)
  8. unescapeHTML(String source)
  9. unescapeHTML(String source)