5

I am getting a string in my C# code that comes 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#?

leora
  • 188,729
  • 360
  • 878
  • 1,366

2 Answers2

13

You 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);
vendettamit
  • 14,315
  • 2
  • 32
  • 54
4

mark it as literal

results.Replace(@"\u0026", "&");
qwertoyo
  • 1,332
  • 2
  • 15
  • 31