Escape and unescape in java

Source: Internet
Author: User
In js, you can use the escape ("") function to convert Chinese characters to unicode encoded strings, that is, the percent sign + u + letters/numbers. When the server side does not automatically decode it, we can write our own functions for transcoding, as shown below:

Public static String escape (String src ){
Int I;
Char j;
StringBuffer tmp = new StringBuffer ();
Tmp. ensureCapacity (src. length () * 6 );
For (I = 0; I <src. length (); I ++ ){
J = src. charAt (I );
If (Character. isDigit (j) | Character. isLowerCase (j)
| Character. isUpperCase (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;
While (lastPos <src. length ()){
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;
}
}
}
Return tmp. toString ();
}

/**
* @ Disc re-encode the string
* @ Param src
* @ Return
*/
Public static String isoToGB (String src ){
String strRet = null;
Try {
StrRet = new String (src. getBytes ("ISO_8859_1"), "GB2312 ");
} Catch (Exception e ){

}
Return strRet;
}

/**
* @ Disc re-encode the string
* @ Param src
* @ Return
*/
Public static String isoToUTF (String src ){
String strRet = null;
Try {
StrRet = new String (src. getBytes ("ISO_8859_1"), "UTF-8 ");
} Catch (Exception e ){

}
Return strRet;
}

Public static void main (String [] args ){
String tmp = "Chinese ";
System. out. println ("testing escape:" + tmp );
Tmp = escape (tmp );
System. out. println (tmp );
System. out. println ("testing unescape:" + tmp );
System. out. println (unescape ("% u6211 % u4eec "));
}

When js uses the escape () function to transcode the Chinese characters in the url, if the server cannot accept this Chinese parameter, use the encodeURI () function instead of the escape () function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.