3

Im trying to replace double backslash for simple one but it does not work. Here an image:

enter image description here

I still wondering why. I saw post like this and it worked for them. Suggestions?

Update

Sorry i made a mistake, its a single backslashes just i spelled wrong the code to replace is

string clear_patch = path.Replace(@"\\","\");
Community
  • 1
  • 1

5 Answers5

10

It is working. \\ is not escaped whilst inspecting during debugging. Try printing the value to a console window or the webpage.

Edit* You appear to be confused.

Your path doesn't actually contain any double slashes \\, it just looks that way because the single slashes are escaped when viewing the string during debugging. Consider reading up on Escape sequences.

If you want to escape single slashes then don't use a verbatim string literal in your replace e.g. string.Replace("\\", "-") which would replace all single slashes in the path with a hyphen - though I'm not sure why you would want to do this? Note that this will still show the path as having \\ slashes in it whilst inspecting during debugging. Printing to a console window would read:

C:-dev-vsprojects-VerificaionDeFirmas-VerificaionDeFirmas-verificaciondefirmas.txt

DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • It can't be working because a path does not contain double `\\` characters. – Myrtle Apr 04 '13 at 14:06
  • 2
    @Aphelion - The debugger automatically escapes backslashes by adding an extra "\" character. – keyboardP Apr 04 '13 at 14:07
  • @keyboardP I know but @'\\' will search for 2 of those characters. I assume as it is a path, the actual path does NOT contain double characters. Then why use the replace? – Myrtle Apr 04 '13 at 14:08
  • @Aphelion - That's true but that doesn't mean the code isn't working, it just means it's not doing anything worthwhile. I thought you meant the code isn't working because the debugger is showing two backslashes. – keyboardP Apr 04 '13 at 14:12
3

The watch window is showing you the escaped string. So when it shows \\ the string only contains \.

Your replace is trying to replace \\ because you are using the literal indicator @.

Change the replace code to:

string clear_path = path.Replace("\\", "-");
John Koerner
  • 37,428
  • 8
  • 84
  • 134
1

Use "\\", not @"\\" as this looks for 2 \ characters.

Note that the visualizer always shows the \\ notation for \ actual characters.

Myrtle
  • 5,761
  • 33
  • 47
1

This is what you want to do:

string clear_path = path.Replace(@"\","-");

The representation in the debugger shows \\ but that means escape char and \

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • 2
    @AdrianCarneiro - You fixed it in the edit by adding the "@" but you just edited it and removed it again. You need to escape the "\" either with the verbatim symbol (@) or using "\\". – keyboardP Apr 04 '13 at 14:06
  • 1
    @keyboardP No idea why this was reverted back. It's there again – Adriano Carneiro Apr 04 '13 at 14:12
0

You should not double escape it, either you do:

string clear_path = path.Replace("\\","-")

or

string clear_path = path.Replace(@"\","-")
von v.
  • 16,868
  • 4
  • 60
  • 84