Chris Cooper
Chris Cooper

Reputation: 389

Removing \ out of my String

I am currently attempting to remove unwanted characters from a string within my C# code.

The remove of the \ character, but it is removing too much. Below is a example of the string

"[{ \"attributes\" : { \"SR_ID\" : \"200003172375\",
\"Fath_SR\" : \"EH0036\", \"UPRN\" : \"100100024250\",
\"Eastings\" : \"260376\", \"Northings\" : \"358150\",
\"Disgrifiad\" : \"Test\", \"Date\" : \"25/01/2012\",
\"Time\" : \"11:36\" }, \"geometry\" : { \"x\" : 270315, \"y\" : 345828 } }]"

I am attempting to remove the \ character, but leave the ". The only way I have been able to remove the \ is by using

 sReturn = sReturn.Replace("\"",String.Empty);

But this removes the " character.

I have tried the two attempts below, but for some reason it does not want to do as it told!

 sReturn = sReturn.Replace(@"\",String.Empty);
 sReturn = sReturn.Replace("\\",String.Empty);

Is there a way I could replace the \" with a "?

Upvotes: 2

Views: 123

Answers (5)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241515

Assuming you really DO have \" characters in your string and aren't just looking at an escaped representation, you can simply do:

sReturn = sReturn.Replace("\\\"","\"");

If you want to unescape all possible types of escape sequences, use:

sReturn = Regex.Unescape(sReturn);

But as others have pointed out, you probably don't really have these characters, and are just looking at a representation where it is shown in the escaped form, such as in visual studio debugger.

Upvotes: 1

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

In fact those characters (\) are \" ("). If you print the string you will see there is no '\' character. So you can leave it as is. If you want to compare these spatial information to other, there must be no problem.

Upvotes: 0

Jacob Proffitt
Jacob Proffitt

Reputation: 12768

The slash( \ ) is an escape character letting the compiler know that the next character is supposed to be part of the string. This is an important notation because otherwise the string would end when the compiler gets to the ending quotation mark. Indeed, if you examine the string under different compiler configuration (if you looked at the library in a VB.Net project, for example), you won't see the slash there.

Upvotes: 0

toby
toby

Reputation: 902

The \" is an escaped string sequence. The actual string is just the quote ("), but the backslash is included to indicate that it is escaped and the quote (") is not actually ending the string (it is part of the string, not the character defining the end of the string).

Try printing the string to the console or displaying it in a message box; that will show what the string actually is without showing the escape strings.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460098

Perhaps:

sReturn = sReturn.Replace(@"\", "");

The verbatim string literal enables to use the \ char.

Edit: I've only just seen that you've already tried it. That's just a display issue in Visual Studio.

Upvotes: 1

Related Questions