URL special character encoding in JS and Java

Source: Internet
Author: User

A few days ago encountered the issue of URL special character encoding, here to tidy up:

  Javascript

1. Encoding Escape (String)

Some of these characters are replaced with 16-binary escape sequences.

   

Decode Unescape (String)

This function works by finding a sequence of characters in the form%xx and%uxxxx (x for hexadecimal digits), decoding with Unicode characters \U00XX and \uxxxx replacing such sequences of characters.

    * Note: ECMAScript v3 against using this method, the app uses decodeURI () and decodeuricomponent () to replace it.

2. Code encodeURI (uristring)

Return value: A copy of uristring, where some of the characters will be replaced by a hexadecimal escape sequence.

    * If the URI component contains delimiters, for example? and #, each component should be encoded using the encodeURIComponent () method.

Decoding decodeURI (uristring)

Return value: A copy of uristring, where the hexadecimal escape sequence is replaced by the character they represent.

3. Code encodeURIComponent (uristring)

Return value: A copy of uristring, where some of the characters will be replaced by a hexadecimal escape sequence.

Decoding decodeURIComponent (uristring)

Return value: A copy of uristring, where the hexadecimal escape sequence is replaced by the character they represent.

  * encodeURIComponent () differs from encodeURI ()

    encodeURIComponent (uristring): Assuming that its arguments are part of a URI (such as a protocol, hostname, path, or query string), the encodeURIComponent () function escapes the URI used to delimit Punctuation marks (?) for each part. # etc).

encodeURI (uristring): does not encode delimiters in URIs, such as? such as

  Java

Code: Class Urlencoder 

    Static    string Encode (string s);  static string encode (string s, String enc); // ENC encoding recommended use UTF-8

Decoding: Class Urldecoder

    Static    string decode (string s);  static string decode (string s, String enc); // ENC encoding recommended use UTF-8

* The encoding and decoding methods in Java and the encodeURIComponent ()/decodeuricomponent () method in JS are common to the front and back, and there is no difference in the way in which the strings are processed;

URL special character encoding in JS and Java

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.