Escape characters in Scala string literals
How Scala escape sequences, triple-quoted strings and the s, f and raw interpolators work, what they change, and when an escaped or plain literal is the better choice.
Escaping a string for Scala source code changes how characters are written in the code. It does not change what the program stores at runtime. That difference decides whether an escaping step or an escaping tool is fit for your purpose. This review covers the standard escape sequences, the alternatives Scala offers (triple-quoted strings and the s, f and raw interpolators), and the points where escaped text stops being readable or portable.
Is escaping the right transformation for your use?
Escape a string when it has to sit inside an ordinary double-quoted Scala literal and contains a double quote, a backslash, a control character such as a newline or tab, or an invisible or ambiguous Unicode character. The benefit is that the literal compiles and means exactly what you intended. The cost is readability: each backslash in the content becomes two in the source, so dense escaping makes the literal hard to review.
Plain text, meaning a triple-quoted or raw literal with little or no escaping, is usually better for regular expressions, Windows paths, embedded SQL, JSON or other multi-line text, and any content that is mostly backslashes or quotes. Escaping is also the wrong tool when the target is not Scala source. JSON, shell commands, HTML and CSV each have their own quoting rules, and a Scala-escaped string is not automatically valid in any of them.
What escaping actually changes
An escape sequence is a notation the compiler reads. The two characters backslash and n in the source become a single line-feed character in the compiled string. Nothing is reversed, mirrored or restyled. The runtime value holds ordinary characters, and the backslashes exist only in the source file. If a string printed at runtime shows a visible backslash, either the source escaped the backslash itself or the literal was a raw form that skipped escape processing.
Standard double-quoted Scala literals recognize the following escapes:
| Sequence | Resulting character |
| \b | Backspace (U+0008) |
| \t | Horizontal tab (U+0009) |
| \n | Line feed (U+000A) |
| \f | Form feed (U+000C) |
| \r | Carriage return (U+000D) |
| " | Double quote |
| ' | Single quote |
| \ | Backslash |
| \uXXXX | The UTF-16 code unit with hex value XXXX |
A backslash followed by any other character is a compile error, so a tool that leaves an unrecognized sequence such as \d in an ordinary literal produces code that will not compile. Octal escapes such as \0 were deprecated in Scala 2 and are rejected by current compilers. Write \u0000 instead. Character literals use the same escapes, so a single quote is written ''' and a backslash is written '\'.
val path = "C:\\Users\\dev" // C:\Users\dev
val quote = "She said \"hi\"" // She said "hi"
val cols = "name\tage" // name<TAB>age
val nul = "\u0000" // one NUL character
Unicode escapes and characters outside the BMP
Scala strings are Java strings, which store UTF-16 code units, and \u takes exactly four hex digits. A character above U+FFFF, such as most emoji, therefore needs two escapes forming a surrogate pair. The emoji U+1F600 is written "\uD83D\uDE00". An escaping tool that writes it as a single escape with five or more digits, or that escapes only half of the pair, produces wrong or uncompilable output. Escaping does not reorder characters, so combining marks and multi-code-point emoji sequences stay intact as long as every code unit is carried through in order.
Where \u escapes are processed has changed between versions. Older Scala 2 releases translated them anywhere in the source file, as Java does. Scala 2.13.2 and later limit this to literals. Handling inside triple-quoted strings and the raw interpolator also differs by version. If your code relies on \u inside one of those forms, check it against the compiler version you actually use.
Alternatives that reduce escaping
Triple-quoted strings
Text between """ delimiters gets no backslash escape processing. It can contain double quotes and real line breaks. This is the usual choice for regular expressions and multi-line text. Combine it with stripMargin so the source can be indented without adding spaces to the value:
val digits = "\\d{3}-\\d{4}".r // escaped form
val same = """\d{3}-\d{4}""".r // triple-quoted, same pattern
val sql = """SELECT *
|FROM users
|WHERE name = 'O''Brien'""".stripMargin
The tradeoff is that a triple-quoted string cannot represent a tab or newline with \t or \n. Those are kept as two literal characters. Real whitespace in the source also becomes part of the value, including trailing spaces and the file's line endings, which an editor or version control setting may change.
Interpolators: s, f and raw
- s"..." processes standard escapes and substitutes $name or ${expression}. A literal dollar sign is written $$.
- f"..." processes escapes like s and adds printf-style format specifiers. A literal percent sign is written %%.
- raw"..." substitutes variables but does not process backslash escapes, so raw"a\nb" contains a backslash and the letter n.
val price = s"Total: $$5" // Total: $5
val pct = f"${0.25 * 100}%.0f%%" // 25%
val re = raw"\d+\.\d+".r // pattern \d+\.\d+
Escaping a double quote inside an interpolated literal has a version history. Older compilers did not accept " there. Newer versions accept $" as an escaped quote inside interpolations. If you must support older compilers, a triple-quoted interpolation such as s"""He said "$name" """ avoids the issue.
Readability and usability after copying or sharing
Escaped source and runtime output are separate things to copy. Copying a literal from source code gives you the escaped notation, backslashes included. Pasting that into a config file, a JSON document or a terminal does not give the intended text unless the destination happens to use the same escape rules. In the other direction, text copied from program output contains the real characters. It has to be escaped again before it can go back into an ordinary literal, and backslashes must be escaped before quotes, or the tool will double-escape its own work.
Escapes matter for review safety. Invisible characters such as zero-width spaces, and bidirectional controls such as U+202E, can make source code display differently from what the compiler reads. Writing them as \u escapes (for example \u200B or \u202E) keeps them visible to reviewers and diff tools. Pasting them raw hides them. For security-sensitive code, an escaping step that converts these characters to explicit escapes adds value. A step that leaves them as raw characters does not.
Accessibility depends on the final output, not on the escape notation. A screen reader reading the running program encounters the actual characters. A screen reader reading source code encounters the backslashes. No assistive-technology test results were available for this review, so how a given screen reader or code editor announces escape sequences is unverified. Test with your own tools if that matters to your team. For search, code search over source code matches the escaped form, so searching for a literal newline or quote may require searching for the escape instead.
Checking an escaping tool or workflow
- Round-trip a sample: escape it, paste it into a Scala literal, print it, and compare the output with the original byte for byte.
- Include a backslash, a double quote, a tab, a newline, a dollar sign (for s strings), a percent sign (for f strings) and one character above U+FFFF.
- Confirm the tool targets Scala specifically. Java output is close but not identical once interpolators are involved, and JSON or JavaScript escaping differs in places.
- Check whether invisible and bidirectional characters come out as \u escapes.
- Check the result against your compiler version, especially for \u in triple-quoted or raw strings and for quotes inside interpolations.
This review is based on documented Scala language behavior. No specific escaping tools were tested and no measurements were available, so claims about any particular converter's accuracy, privacy or data handling would need separate verification. Do not paste secrets or credentials into an online escaping tool without confirming how it handles input.