问题
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(@"\", @"/");
Thanks.
回答1:
You should assign result of replacement:
var res = variable. Replace("\\", "/"); // you need "\\" because "\" is escape symbol.
or
var res = variable.Replace(@"\", "/");
回答2:
As @UweKeim says in a comment, you have to store the result of the Replace
call. Like this:
variable = variable.Replace("@"\", @"/");
回答3:
var newVar = variable.Replace("\\", "/");
来源:https://stackoverflow.com/questions/41935210/replace-all-blackslashes-with-forward-slash