I am getting a string in my C# code that es from some javascript serialization and I see a bunch of strings like this:
Peanut Butter \u0026 Jelly
I tried doing this:
string results = resultFromJsonSerialization();
results = results.Replace("\u0026", "&");
return results;
and I am expecting that to change to:
Peanut Butter & Jelly
but it doesn't seem to do the replace. What is the correct way to do this replacement in C#?
I am getting a string in my C# code that es from some javascript serialization and I see a bunch of strings like this:
Peanut Butter \u0026 Jelly
I tried doing this:
string results = resultFromJsonSerialization();
results = results.Replace("\u0026", "&");
return results;
and I am expecting that to change to:
Peanut Butter & Jelly
but it doesn't seem to do the replace. What is the correct way to do this replacement in C#?
Share Improve this question asked Jun 24, 2015 at 17:53 leoraleora 197k367 gold badges906 silver badges1.4k bronze badges 6- 3 stackoverflow./questions/6990347/… – Rob Schmuecker Commented Jun 24, 2015 at 17:54
- 2 Use another backslash to escape that backslash. "\\u0026" in reality is "\u0026" – Rogue Commented Jun 24, 2015 at 17:56
- 1 Or you can use the string literal symbol e.g. @"\u0026". – Kevin Commented Jun 24, 2015 at 17:58
- @RobSchmuecker no offense here, but why didn't you flag this question as duplicate? – user57508 Commented Jun 24, 2015 at 18:04
- @AndreasNiedermair I'm not au fait enough with C# to make that distinction unfortunately! – Rob Schmuecker Commented Jun 24, 2015 at 18:05
2 Answers
Reset to default 14You can use Regex Unescape() method.
string results = resultFromJsonSerialization();
results = System.Text.RegularExpressions.Regex.Unescape(results);
return results;
You can also utilize the Server utility for HTML encode.
results = ControllerContext.HttpContext.Server.HtmlDecode(results);
mark it as literal
results.Replace(@"\u0026", "&");