Java HTML to String htmlToString(String s)

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

Description

Unescape HTML escape characters in String.

License

Open Source License

Parameter

Parameter Description
s the escaped String

Return

the unescaped String

Declaration

public static String htmlToString(String s) 

Method Source Code

//package com.java2s;
// compliance with the InfoGrid license. The InfoGrid license and important

public class Main {
    /**// ww w .  j a v  a  2 s .c o  m
     * The set of characters we replace in HTML.
     */
    protected static final char[] htmlChars = { '&', '<', '>' };
    /**
     * The set of HTML fragments we replace the characters with.
     */
    protected static final String[] htmlFrags = { "&amp;", "&lt;", "&gt;" };

    /**
     * Unescape HTML escape characters in String.
     *
     * @param s the escaped String
     * @return the unescaped String
     */
    public static String htmlToString(String s) {
        StringBuilder sb = new StringBuilder(s.length());

        int len = s.length();

        for (int i = 0; i < len; ++i) {
            char c = s.charAt(i);
            if (c != '&') {
                sb.append(c);
            } else {
                if (i + 2 < len && s.charAt(i + 1) == '#') { // +2 because we need at least one after the #
                    int semi = s.indexOf('#', i + 1);
                    if (semi >= 0) {
                        char unicode = Character.forDigit(Integer.parseInt(s.substring(i + 2, semi)), 10);
                        sb.append(unicode);

                    } else {
                        sb.append(c); // leave everything normal
                    }
                } else {
                    for (int k = 0; i < htmlFrags.length; ++k) {
                        if (s.regionMatches(i, htmlFrags[k], 0, htmlFrags[k].length())) {
                            sb.append(htmlChars[k]);
                            i += htmlFrags[k].length() - 1; // one less, because the loop increments anyway
                            break;
                        }
                    }
                }
            }
        }
        return sb.toString();
    }
}

Related

  1. html2Plain(String text)
  2. html2Text(String html)
  3. htmlToStr(String htmlStr, int max_count)
  4. htmlToString(String aS_Text)
  5. htmlToString(String string)
  6. htmlToText(String html)
  7. htmlToText(String html)
  8. htmlToText(String input)