Java中JavaScript unescape与escape函数算法
/** * JavaScript escape函数算法 * @param value * @return */ public static String escape(String value){ StringBuffer buffer = new StringBuffer(); int length = value.length(); int ch = 0; for (int i = 0; i < length; i ) { ch = value.charAt(i); if ('A' <= ch && ch <= 'Z')buffer.append((char) ch); else if ('a' <= ch && ch <= 'z')buffer.append((char) ch); else if ('0' <= ch && ch <= '9')buffer.append((char) ch); else if (ch == '-' || ch == '_' || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\'' || ch == '(' || ch == ')')buffer.append((char) ch); else if (ch <= 0x007F) {buffer.append('%');buffer.append(hex[ch]);} else{buffer.append('%');buffer.append('u');buffer.append(hex[(ch >>> 8)]);buffer.append(hex[(0x00FF & ch)]);} } return buffer.toString(); } /** * JavaScript unescape函数算法 * unescape() 函数可对通过 escape() 编码的字符串进行解码。 * @param value * @return */ public static String unescape(String value) { StringBuffer buffer = new StringBuffer(); int i = 0; int length = value.length(); int ch = 0; while (i < length) { ch = value.charAt(i); if ('A' <= ch && ch <= 'Z')buffer.append((char) ch); else if ('a' <= ch && ch <= 'z')buffer.append((char) ch); else if ('0' <= ch && ch <= '9')buffer.append((char) ch); else if (ch == '-' || ch == '_' || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\'' || ch == '(' || ch == ')')buffer.append((char) ch); else if (ch == '%') { int cint = 0; if('u' != value.charAt(i + 1)) { cint = (cint << 4) | val[value.charAt(i + 1)]; cint = (cint << 4) | val[value.charAt(i + 2)]; i = 2; } else { cint = (cint << 4) | val[value.charAt(i + 2)]; cint = (cint << 4) | val[value.charAt(i + 3)]; cint = (cint << 4) | val[value.charAt(i + 4)]; cint = (cint << 4) | val[value.charAt(i + 5)]; i = 5; } buffer.append((char) cint); } else buffer.append((char) ch); i ; } return buffer.toString(); }
如果Java或Android中请求连接带有特殊符号可以使用这两个函数
本博客内容,如需转载请务必保留超链接。Contact Me:Mail此处省略好几个字...