C# String.Replace double quotes and Literals

前端 未结 4 835
孤街浪徒
孤街浪徒 2021-02-02 14:58

I\'m fairly new to c# so that\'s why I\'m asking this here.

I am consuming a web service that returns a long string of XML values. Because this is a string all the attri

4条回答
  •  天命终不由人
    2021-02-02 15:38

    Both the string and the regex uses \ for escaping. The regex will see the character \ followed by ", and think it's a literal escape. Try this:

    Regex rgx = new Regex("\\\\\"");
    string strip = rgx.Replace(xmlSample, "\"");
    

    You could also use raw strings (also known as veratim strings) in C#. They are prefixed with @, and all back-slashes are treated as normal characters. To include a quote in a raw string you need to double it.

    Regex rgx = new Regex(@"\""")
    string strip = rgx.Replace(xmlSample, @"""");

提交回复
热议问题