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".
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
var replaced = originalStr.Replace( "\\", "/" );
回答3:
You need to escape the \
mystring.Replace("\\", "/");
回答4:
var origString = origString.Replace(@"\", @"/");
回答5:
string result = @"c:\asb\def".Replace(Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar);
回答6:
@"C:\abc\def\".Replace(@"\", @"/");
回答7:
string first = @"c:/abc/def";
string sec = first.Replace("/","\\");