EN
Java - escape sequences
0
points
In this article, we would like to show you escape sequences in Java.
Escape sequence | Description |
---|---|
\n | Inserts a new line. |
\t | Inserts a tab. |
\b | Inserts a backspace. |
\r | Inserts a carriage return. |
\f | Inserts a form feed. |
\" | Inserts a double quote character. |
\\ | Inserts a backslash. |
Practical examples
Example 1
In this example, we use \\
escape sequence to insert backslash into the path
String.
public class Example {
public static void main(String[] args) {
String path = "C:\\projects\\example";
System.out.println(path);
}
}
Output:
C:\projects\example
Example 2
In this example, we use \n
escape sequence to insert a new line between the words.
public class Example {
public static void main(String[] args) {
String text = "line1\nline2\nline3";
System.out.println(text);
}
}
Output:
line1
line2
line3
Example 3
In this example, we use \"
escape sequence to insert a double quote character.
public class Example {
public static void main(String[] args) {
String quote = "\"double-quoted text\"";
System.out.println(quote);
}
}
Output:
"double-quoted text"