12

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".

prosseek
  • 182,215
  • 215
  • 566
  • 871

7 Answers7

36

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

You need to escape the \

mystring.Replace("\\", "/");
Bora
  • 283
  • 7
  • 21
2
var replaced = originalStr.Replace( "\\", "/" );
Ed S.
  • 122,712
  • 22
  • 185
  • 265
1
var origString = origString.Replace(@"\", @"/");
Khepri
  • 9,547
  • 5
  • 45
  • 61
1
string first = @"c:/abc/def";
string sec = first.Replace("/","\\");
animuson
  • 53,861
  • 28
  • 137
  • 147
kmerkle
  • 58
  • 1
  • 2
  • 7
0
@"C:\abc\def\".Replace(@"\", @"/");
prosseek
  • 182,215
  • 215
  • 566
  • 871
msarchet
  • 15,104
  • 2
  • 43
  • 66
0
string result = @"c:\asb\def".Replace(Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar);