Scala Escape Characters

In Scala, escape characters are used to represent special characters that cannot be written directly in string literals.

Escape characters start with a backslash\followed by a specific character, indicating some special meaning or effect.

The following table lists common escape characters:

Escape CharacterDescription
\bBackspace
\tTab
\nLine Feed
\fForm Feed
\rCarriage Return
\"Double quote
\'Single quote
\\Backslash

In characters or strings, Unicode characters from 0 to 255 can be represented by an octal escape sequence, i.e., a backslash\followed by up to three octal digits.

If the character sequence after the backslash\does not form a valid escape sequence, a compilation error will occur.

The following example demonstrates the use of some escape characters:

Example

object Test {
   def main(args: Array[String]) {
      println("Hello\tWorld\n\n" );
   }
}

Run example »

The output after executing the above code is as follows:

$ scalac Test.scala
$ scala Test
Hello    World

The following is a Scala program example demonstrating the use of various escape characters:

Example

object EscapeCharacterExamples {
  def main(args: Array[String]): Unit = {
    val backspace = "Hello\bWorld"     // "HelloWorld"
    val tab = "Hello\tWorld"           // "Hello    World"
    val newline = "Hello\nWorld"       // "Hello
                                       // World"
    val formFeed = "Hello\fWorld"      // "Hello World"
    val carriageReturn = "Hello\rWorld"// "World"
    val doubleQuote = "He said, \"Hello, World!\""  // "He said, "Hello, World!""
    val singleQuote = '\"'             // '"'
    val backslash = "This is a backslash: \\"  // "This is a backslash: \"

    // Output example
    println(s"Backspace: $backspace")
    println(s"Tab: $tab")
    println(s"Newline: $newline")
    println(s"FormFeed: $formFeed")
    println(s"CarriageReturn: $carriageReturn")
    println(s"DoubleQuote: $doubleQuote")
    println(s"SingleQuote: $singleQuote")
    println(s"Backslash: $backslash")
  }
}

After executing the above code, the output result is as follows:

、
Backspace: HelloWorld
Tab: Hello    World
Newline: Hello
World
FormFeed: HelloWorld
CarriageReturn: World
DoubleQuote: He said, "Hello, World!"
SingleQuote: "
Backslash: This is a backslash: \

Example explanation:

  • \b(Backspace): Moves the cursor back one position, but does not delete the character. For example,"Hello\bWorld"the result isHelloWorld, where \b here does not delete "o", so the actual display effect depends on the terminal or output device.

  • \t(Tab): Inserts a horizontal tab, equivalent to a certain number of spaces."Hello\tWorld"will insert a tab between "Hello" and "World", usually four or eight spaces.

  • \n(Line Feed): Move to the next line to start new output."Hello\nWorld"will display "Hello" and "World" on two separate lines.

  • \f(Form Feed): Inserts a form feed character, usually used to control printer page breaks, but generally has no obvious effect in console output.

  • \r(Carriage Return): Return to the beginning of the current line."Hello\rWorld"will overwrite the content of the current line, displaying "World".

  • \"(Double quote): Inserts a double quote."He said, \"Hello, World!\""displaysHe said, "Hello, World!"。

  • \'(Single quote): Inserts a single quote. Usually used in character literals, such as'\'\''represents a single quote character.

  • \\(Backslash): Inserts a backslash."This is a backslash: \\"displaysThis is a backslash: \。

Other extensions