Cranialsurge
Cranialsurge

Reputation: 6184

How do I include escape sequences in a string variable

I have a type with a string property called 'TestValue' that contains a string that includes characters that are escape sequences for c#. e.g. "blah\"". I need these characters to print out to the console as well.

Console.WriteLine(obj.TestValue); // prints only blah"

When I hover over that variable in the debugger I can see the exact string "blah\"", but when printed out to the console, only blah" shows up, I want blah\" to show up.

How can I get this to work?

Upvotes: 3

Views: 17146

Answers (4)

Muad'Dib
Muad'Dib

Reputation: 29216

use \ to escape any characters. so, if you want to print the \ character, use \\

Upvotes: 0

Homam
Homam

Reputation: 23841

You can find in this page the escape sequences which include Backslash and Double quotation mark. So, you have to escape them both by a backslash like the following:

Console.WriteLine("Blah\\\"");

Upvotes: 1

Anthony Pegram
Anthony Pegram

Reputation: 126854

Choose one: @"blah\""" or "blah\\\""

@ denotes a verbatim string, but you still need to escape a double quote by doubling it up. Or you can simply escape the \ by doubling it.

Upvotes: 3

Related Questions