In C #, how can I replace &?
I am getting a line in my C # code that comes from some javascript serialization and I see a bunch of lines like this:
Peanut Butter \u0026 Jelly
I tried to do this:
string results = resultFromJsonSerialization();
results = results.Replace("\u0026", "&");
return results;
and I expect this to change to:
Peanut Butter & Jelly
but it doesn't seem to be a replacement. What is the correct way to do this replacement in C #?
+3
source to share
2 answers
You can use the Regex Unescape () method.
string results = resultFromJsonSerialization();
results = System.Text.RegularExpressions.Regex.Unescape(results);
return results;
You can also use the server utility to encode HTML.
results = ControllerContext.HttpContext.Server.HtmlDecode(results);
+3
source to share