最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net Escape() 和 Unescape()用法详解
时间:2022-06-25 04:14:02 编辑:袖梨 来源:一聚教程网
Escape
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。
该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。其他所有的字符都会被转义序列替换。
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
unescape() 函数可对通过 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();
}
该函数的工作原理是这样的:通过找到形式为 %xx 和 %uxxxx 的字符序列(x 表示十六进制的数字),用 Unicode 字符 u00xx 和 uxxxx 替换这样的字符序列进行解码。
相关文章
- 塞尔达传说王国之泪提马瓦卡神庙怎么过 05-07
- 奇迹暖暖沐光恩典怎么样 05-07
- 奇迹暖暖旧宴恩典怎么玩 05-07
- 以闪亮之名迟夜巡演怎么样 05-07
- Swrv币虚拟货币 05-07
- bzone交易平台 05-07