How to replace backslash '\\' with slash &

2019-07-16 04:09发布

问题:

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.