【问题标题】:How to convert a series of unicode characters into readable text?如何将一系列 unicode 字符转换为可读文本?
【发布时间】:2014-10-04 22:14:49
【问题描述】:

这是一个示例输入:"\\u0434\\u0430\\u043C\\u043E",我想将其转换为可读文本。如果它仍然可以有重音字符,我将不胜感激。输入实际上可以比这更长,但这可以用作样本。

是的,我看到了 (http://www.joelonsoftware.com/articles/Unicode.html) 和 (How to print/store non-ASCII characters (unicode?)),但它没有回答我的问题,所以请不要将其标记为重复。我很高兴获得 C# 中的示例代码。 我也尝试过 HttpUtility.HtmlDecode() 但它实际上并没有解码它。 代码如下:

//this is coming from service call and its comming just like this.
var str="\\u0434\\u0430\\u043C\\u043E"; 
var decoded = HttpUtility.HtmlDecode(str); // this doesn't work. Its returning the string str as is.

附带说明:以下将起作用。但我的输入不是那种形式。

//Although my input isn't in the following form, the following works. But my input isn't in this form.
var str2="\u0434\u0430\u043C\u043E";
var decoded = HttpUtility.HtmlDecode(str2);

我怎样才能将像“”\u0434\u0430\u043C\u043E”这样的字符串正确解码为可读文本。

【问题讨论】:

  • @AlexeiLevenkov,HttpUtility.HtmlDecode(str) 按原样返回我 str 而不对其进行解码。这是代码: var str="\\u0434\\u0430\\u043C\\u043E"; var 解码 = HttpUtility.HtmlDecode(str);请记住,我从服务调用中获取 str 并具有如上所示的转义字符。 HttpUtility 将如何提供帮助?
  • 我有错误的建议...重新打开。你能澄清一下你输入的样本是@"\u0434"还是"\u0434"
  • 也许这就是你要找的 - stackoverflow.com/questions/13764168/…
  • 终于有个朋友让我成功了。事实证明我必须使用 Regex.Unscape() 方法。像这样: var str = "\\u0434\\u0430\\u043C\\u043E"; var decoded = HttpUtility.HtmlDecode(Regex.Unescape(str));
  • 您应该将答案作为答案发布并接受它,不要将答案作为问题的一部分发布,因为不清楚问题是否有答案。 ...或者投票关闭作为stackoverflow.com/questions/8558671/…之类的重复项

标签: c# unicode ascii


【解决方案1】:

我终于搞定了:

我通过使用 Regex.Unscape() 方法让它工作。如果其他人遇到同样的问题,问题的解决方法如下:

  var str = "\\u0434\\u0430\\u043C\\u043E";
  var decoded = HttpUtility.HtmlDecode(Regex.Unescape(str)); //take a look the Regex.Unscape() call.

【讨论】:

    猜你喜欢
    • 2019-05-12
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2018-09-19
    • 2011-02-03
    • 2015-12-26
    • 2011-10-04
    • 2019-04-19
    相关资源
    最近更新 更多