C#字符串Unicode转义序列编解码
C#字符串Unicode转义序列编解码
在开发过程中时常会遇到"\Uxxxx"格式表⽰的字符,实际上"xxxx"是字符的Unicode码的⼗六进制表⽰⽅式。这种表⽰称为"Unicode转义字符"。
例如"A"对应的Unicode码为65(⼗进制),转换后为"\U0041"。
以下C#封装的两个扩展函数,可以对Unicode字符串⽂本进⾏转义编码以及从转义序列解码。
1.解码:
public static string UnescapeUnicode(this string str)  // 将unicode转义序列(\uxxxx)解码为字符串
{
return (System.Text.RegularExpressions.Regex.Unescape(str));
}
2.编码:
public static string EscapeUnicode(this string str)  // 将字符串编码为unicode转义序列(\uxxxx)
{
StringBuilder tmp = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
ushort uxc = (ushort)str[i];
tmp.Append(@"\u" + uxc.ToString("x4"));
}
return (tmp.ToString());
unicode所有字符}
参考:
blog.csdn/zcr_59186/

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。