12

I have a collection of strings within a checkListBox and I convert this collection into a List<string>. During this conversion I can only imagine the strings are escaped due to them being in the below format:

<category title="FOO">

This then becomes

"<category title=\"FOO\">

I need to unescape these strings for comparison, and I've tried something like

 s.Replace(@"\""", @""""); <-------- trying to replace all \" with "

Is this even possible? And if so what's the correct way of removing slashes from quotes in a string?

Bradley
  • 1,234
  • 1
  • 12
  • 24

3 Answers3

31

You can use Unescape

    var str = "<category title=\"FOO\">";
    var result = System.Text.RegularExpressions.Regex.Unescape(str);
    Console.WriteLine(result); //<category title="FOO">

    Console.ReadLine();
3615
  • 3,787
  • 3
  • 20
  • 35
2

You can use Regex.Unescape Method to resolve. https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.unescape(v=vs.110).aspx

Or you can use Uri.UnescapeDataString Method.

https://msdn.microsoft.com/en-in/library/system.uri.unescapedatastring(v=vs.110).aspx

Jignesh Patel
  • 189
  • 1
  • 16
-1

Try Replace("\\"", "\""), or even better Replace("\", "")

Eugene
  • 301
  • 1
  • 4
  • 13
  • Just realised that the string doesn't contain a closing tag, so your second suggestion would work providing the slash doesn't interfere with the second quote (which it does unfortunately) – Bradley Jun 17 '16 at 14:35