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.