简体   繁体   English

String.replace“\\\\”with“/”

[英]String.replace “\\” with “/”

There seem to be a lot of questions around this but none of the ones I found seemed to work for me. 围绕这个似乎有很多问题,但我找到的那些似乎都不适合我。

My code: 我的代码:

string subFolderName = category = "Parent/Sub\\Sub sub";
string category = subFolderName.Replace(@"\\", @"/");

This returns category as the same string as subFoldername , ie: 这将category返回为与subFoldername相同的字符串,即:

"Parent/Sub\\Sub sub".

What I actually want is: 我真正想要的是:

"Parent/Sub/Sub sub"

Just try 你试一试

string category = subFolderName.Replace(@"\", @"/");

It will work, because category = "Parent/Sub\\\\Sub sub"; 它会起作用,因为category = "Parent/Sub\\\\Sub sub"; contains a single \\ 包含一个\\

As Damien_The_Unbeliever said in his comment , when you write "Parent/Sub\\\\Sub sub" as a string, actually it contains only one \\ character. 正如Damien_The_Unbeliever在他的评论中所说,当你将"Parent/Sub\\\\Sub sub"写成一个字符串时,它实际上只包含一个\\字符。 So, String.Replace method can't find \\\\ in your string. 所以, String.Replace方法在你的字符串中找不到\\\\

When you use verbatim string literal , your string will be exactly how you wrote it. 当您使用逐字字符串文字时 ,您的字符串将完全是您编写它的方式。

string subFolderName = category = @"Parent/Sub\\Sub sub";
string category = subFolderName.Replace(@"\\", @"/");
Console.WriteLine(category);

Outpuw will be; Outpuw将是;

Parent/Sub/Sub sub

Here is a DEMO . 这是一个DEMO

How are you looking at the contents of category? 你是如何看待类别的内容的? If you are using the VS debugger then it will escape the string so \\ in the string will appear as \\\\ 如果您正在使用VS调试器,那么它将转义字符串,因此字符串中的\\将显示为\\\\

so you either need 所以你要么需要

string category = subFolderName.Replace(@"\", @"/");

or 要么

string category = subFolderName.Replace("\\", "/");
string category = subFolderName.Replace(@"\", "/");

用这个。

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

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