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. |
In this example, we use \\
escape sequence to insert backslash into the path
String.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
String path = "C:\\projects\\example";
5
System.out.println(path);
6
}
7
}
8
Output:
xxxxxxxxxx
1
C:\projects\example
In this example, we use \n
escape sequence to insert a new line between the words.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
String text = "line1\nline2\nline3";
5
System.out.println(text);
6
}
7
}
Output:
xxxxxxxxxx
1
line1
2
line2
3
line3
In this example, we use \"
escape sequence to insert a double quote character.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
String quote = "\"double-quoted text\"";
5
6
System.out.println(quote);
7
}
8
}
Output:
xxxxxxxxxx
1
"double-quoted text"