2009年9月12日

C# JS的Escape和UnEscape *0*

public static string Escape(string str)
{
if (str == null)
return String.Empty;
StringBuilder sb = new StringBuilder();
int len = str.Length;

for (int i = 0; i <len; i++)
{
char c = str[i];

//everything other than the optionally escaped chars _must_ be escaped
if (Char.IsLetterOrDigit(c) || c == '-' || c == '_' || c == '/' || c == '\\' || c == '.')
sb.Append(c);
else
sb.Append(Uri.HexEscape(c));
}

return sb.ToString();
}

public static string UnEscape(string str)
{
if (str == null)
return String.Empty;

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();
}

沒有留言:

張貼留言

您可以使用一些 HTML 標記,例如 <b>, <i>, <a>