简体   繁体   中英

Replacing all the '\' chars to '/' with C#

How can I replace all the '\\' chars in a string into '/' with C#? For example, I need to make @"c:/abc/def" from @"c:\\abc\\def".

The Replace function seems suitable:

string input = @"c:\abc\def";
string result = input.Replace(@"\", "/");

And be careful with a common gotcha:

Due to string immutability in .NET this function doesn't modify the string instance you are invoking it on => it returns the result.

你需要逃避\\

mystring.Replace("\\", "/");
var replaced = originalStr.Replace( "\\", "/" );
var origString = origString.Replace(@"\", @"/");
@"C:\abc\def\".Replace(@"\", @"/");
string result = @"c:\asb\def".Replace(Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar);
string first = @"c:/abc/def";
string sec = first.Replace("/","\\");

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