当前位置: 编程技术>.net/c#/asp.net
C#版的 Escape() 和 Unescape() 函数分享
来源: 互联网 发布时间:2014-10-14
本文导语: 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(); } Unescap...
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("");
代码如下:
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("");