When the B / S program is often a headache for Chinese character encoding, I have always wanted to write a Java version of Escape / Unescape to write a Java version. I finally couldn't bear it last night. I wrote one. There is no time to strictly test, I have to find Bug Mail to give me (stoneat163@163.net)
Class escapeunescape {public static string escape (string src) {INT i; char j; stringbuffer tmp = new stringbuffer (); tmp.ensurecapacity (src.Length () * 6);
For (i = 0; i J = Src.Charat (i); IF (character.isdigit (j) | |arac isiserererererererer;;;;;;;;;;;; (j)) TMP.APpend (j); Else IF (j <256) {tmp.Append ("%"); if (j <16) TMP.Append ("0"); TMP.Append (Integer.Tostring (J, 16));} else {tmp.Append ("% u"); tmp.Append (Integer.toString (J , 16));}} Return Tmp.toString (); Public static string unescape (string src) {stringbuffer tmp = new stringbuffer (); tmp.ensurecapacity (src.Length ()); int lastpos = 0, POS = 0; char ch ;LSTH ()) {POS = src.indexof ("%", lastpos); if (pOS == lastpos) {IF (src.charat (POS 1) == 'u') {ch = (char) integer.parseint (SRC. Substring (POS 2, POS 6), 16); TMP.Append (ch); LastPos = POS 6;} else {ch = (char) integer.parseint (Src.Substring (POS 1, POS 3 ), 16); tmp.Append (ch); LastPos = POS 3;}} else {if (pOS == -1) {TMP.Append (src.substring (lastpos)); LastPos = src.length () } Else {tmp.Append (src.substring (lastpos, pOS)); LastPos = POS;}}}}}}