简体   繁体   中英

Replacing a specific character with string.replace()

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

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

but it replaces / to \\\\ . 即时通讯

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

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#

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM