Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unescaping strings in c# using string.Replace?

Tags:

c#

escaping

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?

like image 927
Bradley Avatar asked Jun 17 '16 14:06

Bradley


3 Answers

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();
like image 131
3615 Avatar answered Nov 20 '22 02:11

3615


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

like image 26
Jignesh Patel Avatar answered Nov 20 '22 02:11

Jignesh Patel


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

like image 1
Eugene Avatar answered Nov 20 '22 02:11

Eugene