C# Unicode 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11700800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:53:11  来源:igfitidea点击:

Unicode characters string

c#unicode

提问by Marc Andreson

I have the following Stringof characters.

我有以下String字符。

string s = "\u0625\u0647\u0644";

When I print the above sequence, I get:

当我打印上述序列时,我得到:

\u0625\u0647\u062

How can I get the real printable Unicode characters instead of this \uxxxx representation?

我怎样才能得到真正的可打印 Unicode 字符而不是这个 \uxxxx 表示?



I have found the answer:

我找到了答案:

s = System.Text.RegularExpressions.Regex.Unescape(s);

回答by Ria

Try Regex:

尝试Regex

String inputString = "\u0625\u0647\u0644";

var stringBuilder = new StringBuilder();
foreach (Match match in Regex.Matches(inputString, @"\u([\dA-Fa-f]{4})"))
{
    stringBuilder.AppendFormat(@"{0}", 
                               (Char)Convert.ToInt32(match.Groups[1].Value));
}

var result = stringBuilder.ToString();

回答by Joey

If you really don't control the string, then you need to replace those escape sequences with their values:

如果你真的不控制字符串,那么你需要用它们的值替换这些转义序列:

Regex.Replace(s, @"\u([0-9A-Fa-f]{4})", m => ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString());

and hope that you don't have \\escapes in there too.

并希望你\\在那里也没有逃脱。

回答by dierre

I would suggest the use of String.Normalize. You can find everything here:

我建议使用String.Normalize. 你可以在这里找到一切:

http://msdn.microsoft.com/it-it/library/8eaxk1x2.aspx

http://msdn.microsoft.com/it-it/library/8eaxk1x2.aspx