public static String decode(String html){
  int ndx=html.indexOf('&');
  if (ndx == -1) {
    return html;
  }
  StringBuilder result=new StringBuilder(html.length());
  int lastIndex=0;
  int len=html.length();
  mainloop:   while (ndx != -1) {
    result.append(html.substring(lastIndex,ndx));
    lastIndex=ndx;
    while (html.charAt(lastIndex) != ';') {
      lastIndex++;
      if (lastIndex == len) {
        lastIndex=ndx;
        break mainloop;
      }
    }
    if (html.charAt(ndx + 1) == '#') {
      char c=html.charAt(ndx + 2);
      int radix;
      if ((c == 'x') || (c == 'X')) {
        radix=16;
        ndx+=3;
      }
 else {
        radix=10;
        ndx+=2;
      }
      String number=html.substring(ndx,lastIndex);
      int i=Integer.parseInt(number,radix);
      result.append((char)i);
      lastIndex++;
    }
 else {
      String encodeToken=html.substring(ndx + 1,lastIndex);
      char[] replacement=ENTITY_MAP.get(encodeToken);
      if (replacement == null) {
        result.append('&');
        lastIndex=ndx + 1;
      }
 else {
        result.append(replacement);
        lastIndex++;
      }
    }
    ndx=html.indexOf('&',lastIndex);
  }
  result.append(html.substring(lastIndex));
  return result.toString();
}

public static String decode ( String html ) { int ndx = html . indexOf ( '&' ) ; if ( ndx == - 1 ) { return html ; } StringBuilder result = new StringBuilder ( html . length ( ) ) ; int lastIndex = 0 ; int len = html . length ( ) ; mainloop : while ( ndx != - 1 ) { result . append ( html . substring ( lastIndex , ndx ) ) ; lastIndex = ndx ; while ( html . charAt ( lastIndex ) != ';' ) { lastIndex ++ ; if ( lastIndex == len ) { lastIndex = ndx ; break mainloop ; } } if ( html . charAt ( ndx + 1 ) == '#' ) { char c = html . charAt ( ndx + 2 ) ; int radix ; if ( ( c == 'x' ) || ( c == 'X' ) ) { radix = 16 ; ndx += 3 ; } else { radix = 10 ; ndx += 2 ; } String number = html . substring ( ndx , lastIndex ) ; int i = Integer . parseInt ( number , radix ) ; result . append ( ( char ) i ) ; lastIndex ++ ; } else { String encodeToken = html . substring ( ndx + 1 , lastIndex ) ; char [ ] replacement = ENTITY_MAP . get ( encodeToken ) ; if ( replacement == null ) { result . append ( '&' ) ; lastIndex = ndx + 1 ; } else { result . append ( replacement ) ; lastIndex ++ ; } } ndx = html . indexOf ( '&' , lastIndex ) ; } result . append ( html . substring ( lastIndex ) ) ; return result . toString ( ) ; }
decodes html text . assumes that all character references are properly closed with semi - colon .
