Why strings need escapes
A Scala string literal starts and ends with a double quote. That makes the double quote a special character: inside a string, it means "the string stops here". So this line can't print the message I am John "the coder" Doe:
println("I am John "the coder" Doe")
The compiler sees the string "I am John ", then the bare words the coder, then a second string starting at the next quote. The code doesn't compile.
The fix is an escape sequence: a backslash \ followed by a character. The backslash tells the compiler to treat the next character differently from usual.
Escaping a double quote
Put a backslash before each quote that belongs to the text:
println("I am John \"the coder\" Doe")
println("After \"Coding 101\" we'll solve some problems")
The output is:
I am John "the coder" Doe
After "Coding 101" we'll solve some problems
The sequence \" in the source code is a single character in the string. The apostrophe in we'll needs no escape inside a double-quoted string, since only the double quote ends such a string.
Escaping the backslash itself
The backslash is special too, so a literal one has to be escaped. A Windows-style path shows the problem:
println("C:\Users\Andy\Games")
The compiler reads \U, \A and \G as the start of escape sequences that don't exist, and rejects the line with errors such as "invalid escape character". To get one real backslash, write two:
println("C:\\Users\\Andy\\Games")
C:\Users\Andy\Games
Each \\ in the source becomes one \ in the string. The string "a\\b" has three characters, not four.
Newlines and other escapes
Escapes also work the other way: a backslash can give a plain letter a special meaning. \n is a newline:
println("Hello world!\nWhat a good day for coding!\n")
println("Let's have a blast!")
Hello world!
What a good day for coding!
Let's have a blast!
The empty line comes from two newlines in a row: the \n at the end of the first string, and the line break that println always adds after its argument.
The common escape sequences are:
| Sequence | Meaning |
|---|---|
\" | a double quote |
\\ | a backslash |
\n | a new line |
\t | a tab |
An alternative: triple-quoted strings
Scala also has strings written between three double quotes. Backslashes in them are not escapes, and single double quotes inside are allowed:
println("""C:\Users\Andy "x" """)
println("""say "hi"""")
C:\Users\Andy "x"
say "hi"
Notice the space before the closing quotes in the first line: three quotes in a row end the string, so a string can't finish with a quote of its own unless something sits in between. The exercises in this lesson use ordinary strings with escapes.
The exercise
The lesson has three steps, each a single println:
- Quotes. Print
I'll study "Coding 101" on AlgoCademy. Escape the two double quotes aroundCoding 101. The apostrophe inI'llstays as it is. - Two lines from one
println. PrintI will learn thisandNo matter whaton separate lines by putting\nbetween them. - A path. Print
C:\Users\Andy\notion, doubling every backslash.
Here are the three lines together:
println("I'll study \"Coding 101\" on AlgoCademy")
println("I will learn this\nNo matter what")
println("C:\\Users\\Andy\\notion")
The output:
I'll study "Coding 101" on AlgoCademy
I will learn this
No matter what
C:\Users\Andy\notion
Common mistakes
- Forward slash instead of backslash.
/"is not an escape; only\starts one. - Escaping the apostrophe. There is no need inside double quotes. Keep the text as it is.
- Single backslash in a path. The line fails to compile. Every backslash in the printed text needs a partner.
- Counting the extra blank line. A trailing
\nplusprintlnprints an empty line. Add a trailing\nonly if you want that. - Space after the backslash. The backslash must touch the character it escapes.
Key takeaways
- A backslash starts an escape sequence inside a string.
\"prints a double quote,\\prints a backslash,\nstarts a new line.- The source text of an escape is longer than the character it produces.
printlnalways adds its own line break.
