Replace all blackslashes with forward slash [duplicate]

这一生的挚爱 提交于 2019-12-14 03:35:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!