45

I am sent an XML string that I'm trying to parse via an XmlReader and I'm trying to strip out the \" characters.

I've tried

.Replace(@"\", "")
.Replace("\\''", "''")
.Replace("\\''", "\"")

plus several other ways.

Any ideas?

Brampage
  • 6,014
  • 4
  • 33
  • 47
Tim Shults
  • 517
  • 1
  • 5
  • 10

6 Answers6

76

Were you trying it like this:

string text = GetTextFromSomewhere();
text.Replace("\\", "");
text.Replace("\"", "");

? If so, that's the problem - Replace doesn't change the original string, it returns a new string with the replacement performed... so you'd want:

string text = GetTextFromSomewhere();
text = text.Replace("\\", "").Replace("\"", "");

Note that this will replace each backslash and each double-quote character; if you only wanted to replace the pair "backslash followed by double-quote" you'd just use:

string text = GetTextFromSomewhere();
text = text.Replace("\\\"", "");

(As mentioned in the comments, this is because strings are immutable in .NET - once you've got a string object somehow, that string will always have the same contents. You can assign a reference to a different string to a variable of course, but that's not actually changing the contents of the existing string.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    Its a common mistake, but gets made a lot. In C# a `string` is immutable, if you change something about it, such as using the `Replace` method, it will always return a new string. – Anthony Jan 12 '11 at 20:08
  • 10
    Also note that the debugger is escaping strings when displaying them. These extra backslashes can of course not be removed by `.Replace()`. – Olivier Jacot-Descombes Jan 03 '14 at 14:37
12

In .NET Framework 4 and MVC this is the only representation that worked:

Replace(@"""","")

Using a backslash in whatever combination did not work...

SonicVader
  • 129
  • 1
  • 3
10

Try it like this:

Replace("\\\"","");

This will replace occurrences of \" with empty string.

Ex:

string t = "\\\"the dog is my friend\\\"";
t = t.Replace("\\\"","");

This will result in:

the dog is my friend
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
dcp
  • 54,410
  • 22
  • 144
  • 164
  • Also you should get used to String.Empty instead of "" –  Jan 12 '11 at 20:06
  • @VoodooChild - No, it's exactly the correct amount of backslashes. See my code example for proof. – dcp Jan 12 '11 at 20:09
  • 3
    meh on string.empty. it's the same difference. – Greg Jan 12 '11 at 20:09
  • 1
    @kurtnelle - I prefer "", there's nothing wrong with using it. I think it's less verbose than string.empty. – dcp Jan 12 '11 at 20:10
  • If he's assigning the return value, I believe this is correct. You need three backslashes, the first is used as an escape character for the second, the third is used as an escape character for the double quote. Then, of course, you need the second double quote to close out the string literal. – Anthony Jan 12 '11 at 20:10
  • @dcp: well, it seems like I misread the question, I thought he only wanted to remove the backslash, but now I notice a `"` as well – VoodooChild Jan 12 '11 at 20:10
  • 1
    Thanks! Your example helped a lot – Met-u Jun 20 '17 at 10:14
1
\ => \\ and " => \"

so Replace("\\\"","")

Michaël
  • 6,676
  • 3
  • 36
  • 55
0

Replace(@"\""", "")

You have to use double-doublequotes to escape double-quotes within a verbatim string.

Greg
  • 16,540
  • 9
  • 51
  • 97
0

Where do these characters occur? Do you see them if you examine the XML data in, say, notepad? Or do you see them when examining the XML data in the debugger. If it is the latter, they are only escape characters for the " characters, and so part of the actual XML data.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343