Languages
[Edit]
EN

Java - escape sequences

0 points
Created by:
daniell
460

In this article, we would like to show you escape sequences in Java.

Escape sequenceDescription
\nInserts a new line.
\tInserts a tab.
\bInserts a backspace.
\rInserts a carriage return.
\fInserts 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"
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - String documentation

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join