Escape:
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:
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();
}
另外, 在网上看到, 在 .net 中还可以这样来调用:
Microsoft.JScript.GlobalObject.escape("");
Microsoft.JScript.GlobalObject.unescape("");