Frage

I'd like to replace all occurences of / to \ . i used this snippet:

_url =  _url.Replace("/",@"\");

but it replaces / to \\. im

Why this happens? How can i modify the snippet to get a good result

War es hilfreich?

Lösung

Your string most likely already contains a single backslash!

I suspect your string already actually only contains a single backslash, 
but you're looking at it in the debugger which is escaping it for you into
a form which would be valid as a regular string literal in C#.

quoted Jon Skeet from: Replace "\\" with "\" in a string in C#

Andere Tipps

I'm going to guess that you attempted to verify correct operation in the debugger. Visual Studio's debugger tips escape backslash characters, so if you see \\ in the tooltip then the string actually contains only 1 backslash. Click the magnifying glass icon at the end of the tooltip in the debugger to bring up a dialog containing the unescaped text.

Edit: This applies to the watch windows as well, including the part about the magnifying glass at the end.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top