开发者

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

开发者 https://www.devze.com 2023-03-06 18:55 出处:网络
How can I replace all the \'\\\' chars in a string into \'/\' with C#? For example, I need to make @\"c:/abc/def\" from @\"开发者_StackOverflow社区c:\\abc\\def\".The Replace function seems suitable:

How can I replace all the '\' chars in a string into '/' with C#? For example, I need to make @"c:/abc/def" from @"开发者_StackOverflow社区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.


You need to escape the \

mystring.Replace("\\", "/");


var replaced = originalStr.Replace( "\\", "/" );


var origString = origString.Replace(@"\", @"/");


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


string result = @"c:\asb\def".Replace(Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar);


@"C:\abc\def\".Replace(@"\", @"/");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号