代码之家  ›  专栏  ›  技术社区  ›  user3818435

如何将一系列unicode字符转换为可读文本?

  •  1
  • user3818435  · 技术社区  · 10 年前

    以下是示例输入: "\\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“这样的字符串正确解码为可读文本。

    1 回复  |  直到 8 年前
        1
  •  0
  •   user3818435    10 年前

    我终于成功了:

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

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