-2

What is the best way to replace all occurrences of "" with "/" in a string in c#?

I've tried the following options but neither work.

  • variable.Replace("", "/");
  • variable.Replace(@"", @"/");
TylerH
  • 20,799
  • 66
  • 75
  • 101
Curtis McCaw
  • 53
  • 1
  • 6

3 Answers3

7

You should assign result of replacement:

var res = variable. Replace("\\", "/"); //  you need "\\" because "\" is escape symbol.

or

var res = variable.Replace(@"\", "/"); 
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Roman
  • 11,966
  • 10
  • 38
  • 47
2

As @UweKeim says in a comment, you have to store the result of the Replace call. Like this:

variable = variable.Replace("@"\", @"/");
Maarten
  • 22,527
  • 3
  • 47
  • 68
2
var newVar = variable.Replace("\\", "/");   
Ben
  • 514
  • 2
  • 10