-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,816
  • 57
  • 73
  • 92
Curtis McCaw
  • 43
  • 1
  • 6

3 Answers3

6

You should assign result of replacement:

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

or

var res = variable.Replace(@"\", "/"); 
Ehsan Sajjad
  • 60,736
  • 15
  • 100
  • 154
Roman
  • 10,951
  • 10
  • 36
  • 43
2

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

variable = variable.Replace("@"\", @"/");
Maarten
  • 21,887
  • 3
  • 47
  • 66
2
var newVar = variable.Replace("\\", "/");   
Ben
  • 514
  • 2
  • 9