YouTip LogoYouTip

Scala Escape Char

In Scala, escape characters are used to represent special characters that cannot be directly written in string literals. Escape characters start with a backslash followed by a specific character, representing some special meaning or effect. The following table lists common escape characters: | Escape Character | Description | | --- | --- | | `b` | Backspace | | `t` | Tab | | `n` | Line Feed | | `f` | Form Feed | | `r` | Carriage Return | | `"` | Double Quote | | `'` | Single Quote | | `` | Backslash | In characters or strings, Unicode characters between 0 and 255 can be represented using an octal escape sequence, which is a backslash followed by up to three octal digits. If the character sequence after the backslash does not form a valid escape sequence, it will cause a compilation error. The following example demonstrates the use of some escape characters: ## Example object Test { def main(args: Array){ println("Hellot Worldnn"); } } [Run Example Β»](#) Executing the above code produces the following output: $ scalac Test.scala $ scala TestHelloWorld The following is a Scala program example demonstrating the use of various escape characters: ## Example object EscapeCharacterExamples { def main(args: Array): Unit ={ val backspace ="Hellob World"// "HelloWorld" val tab ="Hellot World"// "Hello World" val newline ="Hellon World"// "Hello // World" val formFeed ="Hellof World"// "Hello World" val carriageReturn ="Hellor World"// "World" val doubleQuote ="He said, "Hello, World!""// "He said, "Hello, World!"" val singleQuote ='"'// '"' val backslash ="This is a backslash: "// "This is a backslash: " // Output examples 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") } } Executing the above code produces the following output: 、 Backspace: HelloWorldTab: HelloWorldNewline: HelloWorldFormFeed: HelloWorldCarriageReturn: WorldDoubleQuote: He said, "Hello, World!"SingleQuote: "Backslash: This is a backslash: **Example Explanation:** * **`b` (Backspace)**: Moves the cursor back one position but does not delete characters. For example, `"HellobWorld"` results in `HelloWorld`, where the 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. `"HellotWorld"` inserts a tab between "Hello" and "World", typically four or eight spaces. * **`n` (Line Feed)**: Moves to the beginning of the next line for new output. `"HellonWorld"` displays "Hello" and "World" on two separate lines. * **`f` (Form Feed)**: Inserts a form feed, usually used to control printer page breaks, but generally has no visible effect in console output. * **`r` (Carriage Return)**: Returns to the beginning of the current line. `"HellorWorld"` overwrites the content of the current line, displaying "World". * **`"` (Double Quote)**: Inserts a double quote. `"He said, "Hello, World!""` displays `He 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: "` displays `This is a backslash: `.
← C Function FreelocaleMongodb Renamecollection β†’