EDIT: Now I understand what you're trying to do, it’s not at all surprising that it works. You are not talking about the “internal” representation of a string — you are really asking for C # syntax string literal rules to be applied at runtime.
If you write:
string x = "\u0041";
... which creates a string containing one character ("A"). The fact that in the source code was presented as a Unicode escape sequence does not affect the string. Thus, the specified code is indistinguishable at runtime from:
string x = "A";
Now it sounds like you want to parse a line containing a slash, then u , and then four hexadecimal digits into one character. You will need to do this yourself or find another library that will do this - you should not expect string.Replace do this for you.
In other words, it is important to understand the difference between the data itself and the presentation of the source code data.
You state:
str.Replace("/u00", @"\u00")
leads to "\ u00"
No, it really is. If you print the results to the console, you will see only one backslash.
I strongly suspect that you are looking in a debugger that shows an escaped view.
Demo Code:
using System; class Test { static void Main() { string input = "x/u00y"; string output = input.Replace("/u00", @"\u00"); Console.WriteLine(output);
This code:
str.Replace("/u00", "\u00")
really fails because the string literal "\u00" invalid. This is an inexhaustible sequence of Unicode character characters.
source share