How to replace backslash '\' with slash '/'?

試著忘記壹切 提交于 2019-12-11 01:02:39

问题


In my code I want to replace backslash character \ by a forward slash character / in a string. I've tried the following code:

string str = chosen_file.Replace("/", @"\");

where, chosen_file is a string which contains numerous occurances of the \ character. But it doesn't seem to be working.

Can I know any other solutions for this issue?


回答1:


If you look at the definition of String.Replace:

public string Replace(
    string oldValue,
    string newValue
)

and your call:

chosen_file.Replace("/", @"\");

You are replacing forward slashses with backslashes. If you swap the arguments you should be getting the desired behavior.




回答2:


Double slash will show only at the time of debugging.If you print this value in console application or lable box then you can see the correct answer.



来源:https://stackoverflow.com/questions/24823436/how-to-replace-backslash-with-slash

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