Here you can find the source of unescape(String str)
public static String unescape(String str)
//package com.java2s; //License from project: Apache License public class Main { public static String unescape(String str) { int idxS = str.indexOf('&'); if (idxS < 0) { return str; }//from ww w . j a v a 2s . co m StringBuilder sb = new StringBuilder(str.length()); int idxE, idx, size; char c; int prev = 0; while (idxS != -1) { if (prev < idxS) { sb.append(str.substring(prev, idxS)); prev = idxS; } idxE = str.indexOf(';', idxS); if (idxE < 0) { break; } idx = idxS + 1; size = idxE - idxS - 1; if (size < 2) { sb.append(str.substring(idxS, idxE + 1)); } else { c = str.charAt(idx); switch (c) { case 'l': if (!xmlDecodeLT(str, idx, sb, size)) { sb.append("&"); idxE = idxS; } break; case 'g': if (!xmlDecodeGT(str, idx, sb, size)) { sb.append("&"); idxE = idxS; } break; case 'q': if (!xmlDecodeQUOT(str, idx, sb, size)) { sb.append("&"); idxE = idxS; } break; case 'a': if (!xmlDecodeAMPAPOS(str, idx, sb, size)) { sb.append("&"); idxE = idxS; } break; case 'n': if (!xmlDecodeNBSP(str, idx, sb, size)) { sb.append("&"); idxE = idxS; } break; case '#': if (!xmlDecodeNumber(str, idx, sb, idxE)) { sb.append("&"); idxE = idxS; } break; default: sb.append("&"); idxE = idxS; } } prev = idxE + 1; idxS = str.indexOf('&', prev); } if (prev < str.length()) { sb.append(str.substring(prev)); } return sb.toString(); } private static boolean xmlDecodeLT(String str, int idx, StringBuilder sb, int size) { boolean isRecognized = true; char c = str.charAt(idx + 1); if ((size != 2) || (c != 't')) { isRecognized = false; } else { // lt sb.append('<'); } return isRecognized; } private static boolean xmlDecodeGT(String str, int idx, StringBuilder sb, int size) { boolean isRecognized = true; char c = str.charAt(idx + 1); if ((size != 2) || (c != 't')) { isRecognized = false; } else { sb.append('>'); } return isRecognized; } private static boolean xmlDecodeQUOT(String str, int idx, StringBuilder sb, int size) { boolean isRecognized = true; char c = str.charAt(idx + 1); if ((size != 4) || (c != 'u' || str.charAt(idx + 2) != 'o' || str.charAt(idx + 3) != 't')) { isRecognized = false; } else { sb.append('"'); } return isRecognized; } private static boolean xmlDecodeAMPAPOS(String str, int idx, StringBuilder sb, int size) { boolean isRecognized = true; char c = str.charAt(idx + 1); if ((size == 3) && (c == 'm') && (str.charAt(idx + 2) == 'p')) { sb.append('&'); } else if ((size == 4) && (c == 'p') && (str.charAt(idx + 2) == 'o') && (str.charAt(idx + 3) == 's')) { sb.append('\''); } else { isRecognized = false; } return isRecognized; } private static boolean xmlDecodeNBSP(String str, int idx, StringBuilder sb, int size) { boolean isRecognized = true; char c = str.charAt(idx + 1); if ((size != 4) || (c != 'b' || str.charAt(idx + 2) != 's' || str.charAt(idx + 3) != 'p')) { isRecognized = false; } else { sb.append(' '); } return isRecognized; } private static boolean xmlDecodeNumber(String str, int idx, StringBuilder sb, int idxE) { boolean isRecognized = true; try { char c; char c1 = str.charAt(idx + 1); if (c1 == 'x') { c = (char) Integer.parseInt(str.substring(idx + 2, idxE), 16); } else { c = (char) Integer.parseInt(str.substring(idx + 1, idxE)); } sb.append(c); } catch (NumberFormatException ex) { isRecognized = false; } return isRecognized; } }