Asp.net Escape () and Unescape () Usage

Source: Internet
Author: User

Escape
The escape () function can encode a string so that the string can be read on all computers.

This method does not encode ASCII letters and numbers or the following ASCII punctuation marks :-_.! ~ *'(). All other characters are replaced by escape sequences.

 

Public static string Escape (string str)
{
StringBuilder sb = new StringBuilder ();
Foreach (char c in str)
{
Sb. Append (Char. IsLetterOrDigit (c)
| C = '-' | c = '_' | c =''
| C = '/' | c = '.')? C. ToString (): Uri. HexEscape (c ));
}
Return sb. ToString ();
}

UnEscape

The unescape () function can decode strings encoded by escape.

 

Public static string UnEscape (string str)
{
StringBuilder sb = new StringBuilder ();
Int len = str. Length;
Int I = 0;
While (I! = Len)
{
If (Uri. IsHexEncoding (str, I ))
Sb. Append (Uri. HexUnescape (str, ref I ));
Else
Sb. Append (str [I ++]);
}
Return sb. ToString ();
}

This function works like this: Find the character sequence in the form of % xx and % uxxxx (x indicates a hexadecimal number ), decodes character sequences such as Unicode u00xx and uxxxx.

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.