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".
Asked
Active
Viewed 7.1k times
7 Answers
35
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.
Darin Dimitrov
- 994,864
- 265
- 3,241
- 2,902
0
string result = @"c:\asb\def".Replace(Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar);
Damian Leszczyński - Vash
- 29,574
- 9
- 57
- 93