0

Check the code bellow. I am trying to find \\ and replace with \ single back slash but it seems Replace() method can't do that. I keep getting the same result on test2 variable.

How can I fix this?

string test1 = "C:\\Users\\lik\\Desktop\\foo\\BacklogExcelGenerator\\bin\\Debug\\foo.xlsx";


string test2 = test1.Replace("\\", @"\");
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
John Doe
  • 317
  • 1
  • 3
  • 8
  • 2
    In a C# string \\ is how you represent a single \ so what your attempting is probably not necessary. (If you looked at test1 in the debugger you would again see \\ but in reality - when you print or display the string elsewhere - there is only a single \ present) – Alex K. Dec 07 '18 at 11:49
  • 7
    but `"\\"` *is* a single slash... – Marc Gravell Dec 07 '18 at 11:50
  • 2
    Yes, `test1` *doesn't* contain any double backslashes. Be wary, the C# debugger likes to show you "What you would have to write in C# source code for a string literal" rather than "the exact current contents of the string". So if that's where you're looking and seeing a problem, it's not real. – Damien_The_Unbeliever Dec 07 '18 at 11:51
  • 1
    Usually when expressing a path we would use the `@` symbol to indicate that the contained text is a string literal: `@"C:\Users\lik\Desktop\foo\BacklogExcelGenerator\bin\Debug\foo.xlsx"` – Martin Dec 07 '18 at 11:52
  • @John Doe , I ran the code that you wrote, please make sure again to verify that it is working, it is working for me perfectly. Here is the shot : https://ibb.co/XyLsh49 – affanBajwa Dec 07 '18 at 12:01
  • @affanBajwa I believe the issue is a gap in understanding escape characters and not the code. – Michael Dec 07 '18 at 12:07

3 Answers3

1

\ is used as an escape character in strings. Escape characters are used to encode special "non printed" characters inside a string like \n is new line \" is a quote etc. Because \ is the escape character, in order to write a \ we have to escape it and write it as \\ this shows up as a double slash in code and if you view the string in a debugger but both in memory and when it's printed to the screen it appears as 1.

For instance

string s = "The quick \"brown\" fox jumped\nOver the lazy dog. \\\\o_o//";

will print to the screen as

The quick "brown" fox jumped
Over the lazy dog.\\o_o//

Some light reading on escape sequences and you'll be good to go

Verbatim strings, made in C# by @"" will treat everything as literal and don't have escape characters, if you want a newline you have to write the string over 2 lines. The only escape you can do in a verbatim string is " and that's done by ""

string s = @"The quick ""brown"" fox jumped
Over the lazy dog. \\o_o/";

will have the same output as the escaped string above

The quick "brown" fox jumped
Over the lazy dog.\\o_o//
Michael
  • 810
  • 6
  • 18
0

"\\" and @"\" are equal strings that contain on character slash '\'. So, Your method replaces '\' with '\'.

\ is a special character, that uses for declaring some others values like \n - new line, \t - tab and so on. And single slash is written as \\.

Read more in article Escape Sequences.

Backs
  • 24,430
  • 5
  • 58
  • 85
0

you are missing a @ in the \\ to replace, remember that \ is a escape character so you are only asking for one bar for the compiler in the end you should have something like this to replace the double bar

string test2 = test1.Replace(@"\\", @"\");

In that way you will indicate that the \\ is literal too

ShinyDarkStone
  • 70
  • 1
  • 10
  • 2
    note, however, that in the context of the question as asked: there is no double-slash to replace in the first place – Marc Gravell Dec 07 '18 at 11:52
  • You're right. the string does not have any double slash to replace because is not literal either. Good catch – ShinyDarkStone Dec 07 '18 at 11:54
  • 1
    There is no point telling the OP how to 'improve' / change their second line. The problem isn't with the second line - the problem is that he/she doesn't **need** the second line, and doesn't understand that yet. _The OP's original second line does nothing, while your new code does a slightly different nothing (based on the OP's `test1` value)._ – mjwills Dec 07 '18 at 11:59