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
  • 169,389
  • 197
  • 542
  • 840

7 Answers7

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
2

You need to escape the \

mystring.Replace("\\", "/");
Bora
  • 273
  • 6
  • 20
2
var replaced = originalStr.Replace( "\\", "/" );
Ed S.
  • 119,398
  • 20
  • 176
  • 254
1
var origString = origString.Replace(@"\", @"/");
Khepri
  • 9,387
  • 5
  • 44
  • 61
1
string first = @"c:/abc/def";
string sec = first.Replace("/","\\");
animuson
  • 52,378
  • 28
  • 138
  • 145
kmerkle
  • 58
  • 1
  • 2
  • 7
0
@"C:\abc\def\".Replace(@"\", @"/");
prosseek
  • 169,389
  • 197
  • 542
  • 840
msarchet
  • 14,876
  • 2
  • 42
  • 66
0
string result = @"c:\asb\def".Replace(Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar);