How to unescape unicode string in c #

I have a Unicode string from a text file like this. And I want to show a real symbol.

For example:

\u8ba1\u7b97\u673a\u2022\u7f51\u7edc\u2022\u6280\u672f\u7c7b 

When you read this line from a text file using StreamReader.ReadToLine() , it escapes \ to '\\' , such as "\\u8ba1" , which is not needed.

It will display a Unicode string, the same as the text. I want to show a real character.

  • How to change "\\u8ba1" to "\u8ba1" in the result line.
  • Or should I use another reader to read the line?
+8
c # unicode
source share
2 answers

If you have a string like

 var input1 = "\u8ba1\u7b97\u673a\u2022\u7f51\u7edc\u2022\u6280\u672f\u7c7b"; // input1 == "计算机•网络•技术类" 

you don’t have to do anything. This is just a string literal containing escape sequences, not the string itself.


If you have a string like

 var input2 = @"\u8ba1\u7b97\u673a\u2022\u7f51\u7edc\u2022\u6280\u672f\u7c7b"; 

you can undo it using the following regular expression:

 var result = Regex.Replace( input2, @"\\[Uu]([0-9A-Fa-f]{4})", m => char.ToString( (char)ushort.Parse(m.Groups[1].Value, NumberStyles.AllowHexSpecifier))); // result == "计算机•网络•技术类" 
+14
source share

This question appeared in the first result when searching on googling, but I thought there should be an easier way ... here is what I ended up using:

 using System.Web; //... string x = HttpUtility.UrlDecode("Ingl\u00e9s"); Console.Write(x); // Inglés 
0
source share

All Articles