-4

I want to print something like : Welcome "Aditya"

I tried using escape character

string s = "Welcome \"Aditya\""; 

But it is printing as : Welcome \"Aditya\"

I have even tried to just print backslash(\) using string s = "\\" but instead I got \\.

Can anyone help me understand why escape character is not working or if I am doing anything wrong here?

Aditya
  • 65
  • 10
  • Does this answer your question? [Escape double quotes in string](https://stackoverflow.com/questions/14480724/escape-double-quotes-in-string) – Charmander Jan 07 '20 at 13:00
  • 2
    Your code `string s = "Welcome \"Aditya\"";` is correct; `Console.WriteLine(s);` should output `Welcome "Aditya"`. – mortb Jan 07 '20 at 13:02
  • 8
    I bet you're looking at it in the Watch window in Visual Studio. The Watch window will add backslashes for you. If you put `s,nq` in the watch window, it will stop doing this. – canton7 Jan 07 '20 at 13:03
  • What other is the rest of your code? because this alone is not the problem – Joost K Jan 07 '20 at 13:03
  • I have also tried `string s = @"Welcome ""Aditya"" "` but gives me the same output as I mentioned above.I am getting extra backslash before the quote – Aditya Jan 07 '20 at 13:04
  • 3
    Like @canton7 said, you are probably looking at it in the watch window, and not in a console output (for example) – Nsevens Jan 07 '20 at 13:05
  • 2
    You are correct @canton7 – Aditya Jan 07 '20 at 13:05

2 Answers2

11

I'm guessing you're looking at the Watch window in Visual Studio:

Watch Window Raw

The Watch window adds its own quotes, and puts backslashes before any quotes in the string. This is probably confusing you.

You can click the magnifying glass next to the string in the watch window to see its actual value:

Magnifying glass in watch window

Alternatively, put ,nq (for "no quotes") after the variable name:

nq in watch window

A full list of Debugger Format specifiers is here.

If you're just hovering over the variable name, you can also click the magnifying glass to see the string's actual value:

Magnifying glass in hover

canton7
  • 37,633
  • 3
  • 64
  • 77
2

Just use string literal:

string s = @"Welcome ""Aditya""";
Stemado
  • 599
  • 5
  • 10