Search code examples
c#stringreplacebackslashslash

Replacing a specific character with string.replace()


I'd like to replace all occurences of / to \ . i used this snippet:

_url =  _url.Replace("/",@"\");

but it replaces / to \\. im

Why this happens? How can i modify the snippet to get a good result


Solution

  • Your string most likely already contains a single backslash!

    I suspect your string already actually only contains a single backslash, 
    but you're looking at it in the debugger which is escaping it for you into
    a form which would be valid as a regular string literal in C#.
    

    quoted Jon Skeet from: Replace "\\" with "\" in a string in C#