C# 用字符串替换特定字符。替换()

C# 用字符串替换特定字符。替换(),c#,string,replace,backslash,slash,C#,String,Replace,Backslash,Slash,我想将所有出现的/替换为\。我用了这个片段: _url = _url.Replace("/",@"\"); 但它将/替换为\。 为什么会发生这种情况?如何修改代码段以获得良好的结果您的字符串很可能已经包含一个反斜杠 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

我想将所有出现的
/
替换为
\
。我用了这个片段:

_url =  _url.Replace("/",@"\");
但它将
/
替换为
\


为什么会发生这种情况?如何修改代码段以获得良好的结果

您的字符串很可能已经包含一个反斜杠

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#.

引用Jon Skeet的话:

您的字符串很可能已经包含一个反斜杠

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#.

引用Jon Skeet的话:

我猜您试图在调试器中验证正确的操作。Visual Studio的调试器提示转义反斜杠字符,因此如果在工具提示中看到
\\
,则字符串实际上只包含1个反斜杠。单击调试器中工具提示末尾的放大镜图标,以打开包含未缩放文本的对话框


编辑:这也适用于手表窗口,包括最后关于放大镜的部分。

我猜您试图在调试器中验证正确的操作。Visual Studio的调试器提示转义反斜杠字符,因此如果在工具提示中看到
\\
,则字符串实际上只包含1个反斜杠。单击调试器中工具提示末尾的放大镜图标,以打开包含未缩放文本的对话框


编辑:这也适用于手表窗口,包括末尾关于放大镜的部分。

不正确,只需使用调试器的放大镜图标查看准确的字符串:)不正确,只需使用调试器的放大镜图标查看准确的字符串:)