最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In C#, how can I replaceu0026 with &? - Stack Overflow

programmeradmin6浏览0评论

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
 |  Show 1 more ment

2 Answers 2

Reset to default 14

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);

mark it as literal

results.Replace(@"\u0026", "&");
发布评论

评论列表(0)

  1. 暂无评论