Here you can find the source of htmlToString(String s)
Parameter | Description |
---|---|
s | the escaped String |
public static String htmlToString(String s)
//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 = { "&", "<", ">" }; /** * 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(); } }