56

I am a bit new to c#, and i am stuck at this point, I have a regular string, where i made use of \ to escape ", escape here means that to escape the compilers interpretation of ", and get " printed on the screen, and i get the expected output-->

class Program
{
    static void Main(string[] args)
    {
        string s1 = "This is a \"regular\" string";
        System.Console.WriteLine(s1);
        System.Console.Read();
    }
}

o/p--> This is a "regular" string

Now, i have a verbatim string, and i am trying to escape " using \ in the same manner as above..-->

class Program
{
    static void Main(string[] args)
    {
        string s2 = @"This is \t a \"verbatim\" string";//this would escape \t
        System.Console.WriteLine(s2);
        System.Console.Read();
    }
}

Why the above isn't working ?

Darren
  • 68,902
  • 24
  • 138
  • 144
Srinivas Cheruku
  • 1,314
  • 3
  • 11
  • 19

3 Answers3

73

Use a double quote:

 string s2 = @"This is \t a ""verbatim"" string";

With C# 11 you could also use a raw string literal, for example:

string longMessage = """
    This is a long message.
    It has several lines.
        Some are indented
                more than others.
    Some should start at the first column.
    Some have "quoted text" in them.
    """;

Raw string literals are a new format for string literals. Raw string literals can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences. A raw string literal starts with at least three double-quote (""") characters. It ends with the same number of double-quote characters.

Darren
  • 68,902
  • 24
  • 138
  • 144
18

If you want to write a verbatim string containing a double-quote you must write two double-quotes.

string s2 = @"This is \t a ""verbatim"" string";

enter image description here

This is covered in section 2.4.4.5 of the C# specification:

2.4.4.5 String literals

C# supports two forms of string literals: regular string literals and verbatim string literals.

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
4

In a Verbatim string, backslashes are treated as standard characters, not escape characters. The only character that needs escaping is the quotation marks, which you can escape using the very same character:

string s2 = @"This is \t a ""verbatim"" string";

Of course you can never add special characters like \t (tab) using this method, so it is only useful for simple strings - I think I only ever use this when working with file paths.

musefan
  • 47,875
  • 21
  • 135
  • 185
  • You can add tabs - press the tab button. Same goes for newlines (it will insert \r\n) but other control characters may be more difficult. Copy-paste from character map. – Gusdor Jun 18 '13 at 13:16
  • For file paths there are some things better in C# as Path.DirectorySeparatorChar() method. – sergiol Jun 21 '13 at 11:06
  • 2
    @sergiol: Indeed, and to be honest I don't really use them for file paths either. If I am constructing a file path I would use `Path.Combine()` instead – musefan Jun 21 '13 at 11:15