Unescaping strings in c# using string.Replace?

巧了我就是萌 提交于 2019-12-12 10:59:28

问题


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?


回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/37884167/unescaping-strings-in-c-sharp-using-string-replace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!