Start learning free

LESSON 53 OF 347 · STRINGS I

Escaping Characters in C#: Quotes, Backslashes and \n

Lesson Read the full explanation

There are times when we want to use quotes inside a string. For example, suppose we want to print the message I am John "the coder" Doe:

Console.WriteLine("I am John "the coder" Doe");

You can already see by the text color that we have a problem. C# recognizes the string to be only I am John .

C# finds the first quotation mark and interprets that the string starts there. But then, it finds the second quotation mark (after John ) and interprets the string ends there.

Character escaping comes to our rescue!


Escaping characters:

In C#, some characters serve different functions and so cannot be displayed or printed like other characters. These characters are referred to as special characters.

For example, double quotes (") is considered a special character by C#. Its job is to let C# know when a string declaration starts / ends.

There is a way to print special characters like these in C#, and that is by using the backslash character (\).

When we put \ before a special character, we tell C#:

"Hey, I know this is a special character, but I don't want to use it like that here. I just want to print it like I print any other character."

For example, when putting a \ before a double quote, C# no longer interprets that as being the end of the string:

Console.WriteLine("I am John \"the coder\" Doe");

Console.WriteLine("After \"Coding 101\" we'll solve some problems");

The output of this code is:

I am John "the coder" Doe
After "Coding 101" we'll solve some problems

Escaping backslash:

Sometimes we might want to print strings that contain character \, such as a file path in your PC:

Console.WriteLine("C:\Users\Andy\Games");

This code looks perfectly fine at first sight, but if you run it, it would produce an error: illegal escape character.

When C# sees \U, it supposes that you are trying to escape character U, which is not a special character thus cannot be escaped.

Now that you've learned about its function, what type of character do you think \ is in C#? It's a special character!

And like any special character, it can also be escaped by putting another \ in front of it (\\):

Console.WriteLine("C:\\Users\\Andy\\Games");

The output of this code is:

C:\Users\Andy\Games

New line character:

Similarly, the escape sequence can be used to make an otherwise printable character serve a special function.

For example, the letter n can be printed normally, but adding a backslash before it (\n) will indicate the start of a new line:

Console.WriteLine("Hello world!\nWhat a good day for coding!\n");
Console.WriteLine("Let's have a blast!");

The output of this code is:

Hello world!
What a good day for coding!

Let's have a blast!

Notice the empty line after What a good day for coding! ?

That's one new line from the \n plus the new line that Console.WriteLine() prints no matter what the message is.


Assignment
Follow the Coding Tutorial and let's escape some characters!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    C# · STRINGS I

    Escaping Characters in C#, explained

    What escaping is

    A string literal in C# is text between double quotes. That makes the double quote a special character: it marks where the string starts and ends. So how do you put a quote inside a string? This line looks reasonable but does not compile:

    Console.WriteLine("I am John "the coder" Doe");
    

    The compiler sees the string "I am John " end at the second quote, then finds the word the where it expects something else, and reports syntax errors.

    The fix is an escape sequence: a backslash followed by a character. The backslash tells the compiler to treat the next character in a special way. The two characters you type count as one character in the resulting string.

    The escape sequences you need now

    You writeYou get
    \"a double quote
    \\one backslash
    \na new line
    \ta tab

    Quotes

    Console.WriteLine("I am John \"the coder\" Doe");
    Console.WriteLine("After \"Coding 101\" we'll solve some problems");
    
    I am John "the coder" Doe
    After "Coding 101" we'll solve some problems
    

    Notice the apostrophe in we'll. Inside a double-quoted string, a single quote needs no escape, because only the double quote ends this kind of string. (A single quote does need an escape in a char literal: '\''.)

    Backslashes

    The backslash itself is special, so a file path needs doubled backslashes:

    Console.WriteLine("C:\\Users\\Andy\\Games");
    
    C:\Users\Andy\Games
    

    With a single backslash, "C:\Users" fails to compile with the error "unrecognized escape sequence", because \U is not a valid escape. The danger is greater when the letter after the backslash is a valid escape. In "C:\new", the \n is silently read as a new line, and the program compiles and prints the wrong thing.

    New lines

    Console.WriteLine("Hello world!\nWhat a good day for coding!\n");
    Console.WriteLine("Let's have a blast!");
    
    Hello world!
    What a good day for coding!
    
    Let's have a blast!
    

    The blank line appears because the string ends with \n and Console.WriteLine then adds its own line break. Two line breaks in a row make one empty line.

    Why the compiler needs this

    The compiler reads your code one character at a time, so it has no way to know that a quote in the middle of text is meant as text. Escape sequences remove the guesswork: a backslash is a signal that the next character must be read differently, whether that means "this quote is plain text" or "this letter stands for a line break". The same idea appears in many languages, so it's worth learning once and well.

    A shortcut: verbatim strings

    C# has another kind of literal that avoids most backslash work. Put @ before the opening quote and backslashes are taken literally:

    Console.WriteLine(@"C:\Users\Andy\notion");
    Console.WriteLine(@"say ""hi""");
    
    C:\Users\Andy\notion
    say "hi"
    

    In a verbatim string, a quote is written by doubling it, and \n is not a new line. This lesson uses ordinary strings with escapes, so the exercise below uses those.

    The exercise

    The lesson has three prints:

    1. Quotes. Print I'll study "Coding 101" on AlgoCademy. Escape the two inner double quotes with \". The apostrophe in I'll stays as it is.
    2. Two lines, one statement. Print I will learn this and No matter what on separate lines using a single Console.WriteLine. Put \n between the two phrases.
    3. A path. Print C:\Users\Andy\notion by doubling every backslash.
    Console.WriteLine("I'll study \"Coding 101\" on AlgoCademy");
    Console.WriteLine("I will learn this\nNo matter what");
    Console.WriteLine("C:\\Users\\Andy\\notion");
    

    The output is:

    I'll study "Coding 101" on AlgoCademy
    I will learn this
    No matter what
    C:\Users\Andy\notion
    

    Common mistakes

    • Using a forward slash. /" does not escape anything. The escape character in C# is the backslash.
    • Escaping the wrong quotes. Only the quotes inside the text are escaped. The quotes that open and close the string stay plain.
    • Putting a space after \n. "this\n No matter" starts the second line with a space.
    • Undoubled backslashes in paths. They either fail to compile or turn into escapes such as \n and \t.
    • Thinking \n is two characters. The two characters \ and n exist only in your source code. The string itself holds a single line-break character, so "a\nb" has three characters.

    Key takeaways

    • A backslash inside a string literal starts an escape sequence.
    • \" is a quote, \\ is a backslash, \n is a new line and \t is a tab.
    • An unknown sequence like \U is a compile error.
    • Console.WriteLine adds a line break after its text, in addition to any \n in it.
    • A verbatim string with @ is handy for paths.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free