简体   繁体   English

用string.replace()替换特定字符

[英]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# 引用了Jon Skeet的文章: 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. Visual Studio的调试器提示转义了反斜杠字符,因此,如果在工具提示中看到\\\\ ,则该字符串实际上仅包含1个反斜杠。 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. 编辑 :这也适用于观察窗,包括末尾有关放大镜的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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