How to decode a Unicode character in a string
How do I decode this string 'Sch\u00f6nen' (@"Sch\u00f6nen"
) in C#, I've tried HttpUtility but it doesn't give me the results I need, which is "Schönen".
Solution 1:
Regex.Unescape
did the trick:
System.Text.RegularExpressions.Regex.Unescape(@"Sch\u00f6nen");
Note that you need to be careful when testing your variants or writing unit tests: "Sch\u00f6nen"
is already "Schönen"
. You need @
in front of string to treat \u00f6
as part of the string.
Solution 2:
If you landed on this question because you see "Sch\u00f6nen"
(or similar \uXXXX
values in string constant) - it is not encoding. It is a way to represent Unicode characters as escape sequence similar how string represents New Line by \n
and Return by \r
.
I don't think you have to decode.
string unicodestring = "Sch\u00f6nen";
Console.WriteLine(unicodestring);
Schönen was outputted.