143

I know some of the escape characters in Java, e.g.

\n : Newline
\r : Carriage return
\t : Tab
\\ : Backslash
...

Is there a complete list somewhere?

2
  • 13
    This is in the Java Language Spec
    – skaffman
    Sep 2, 2009 at 12:13
  • 2
    @user207421 though it is nice for the rest of us to find while googling.
    – jaredad7
    Jul 14, 2021 at 18:16

5 Answers 5

215

You can find the full list here.

  • \t Insert a tab in the text at this point.
  • \b Insert a backspace in the text at this point.
  • \n Insert a newline in the text at this point.
  • \r Insert a carriage return in the text at this point.
  • \f Insert a formfeed in the text at this point.
  • \s Insert a space in the text at this point.
  • \' Insert a single quote character in the text at this point.
  • \" Insert a double quote character in the text at this point.
  • \\ Insert a backslash character in the text at this point.
7
  • 33
    The list is missing Unicode and octal escapes: \u1234 \012 \01 \0
    – Sampo
    Apr 30, 2014 at 13:04
  • 5
    Here is the paragraph for the unicode escape sequence in the java spec
    – JE42
    Sep 22, 2014 at 21:31
  • 9
    \a does not compile in javac 1.8.0_20: illegal escape character: String test = "\a";
    – Ehryk
    Mar 15, 2015 at 18:18
  • 3
    "Unicode escapes are pre-processed before the compiler is run." -- Mark Peters. So they are different from the standard String escapes listed here. Thanks Jan for the comment to this answer Sep 23, 2015 at 20:34
  • 2
    When speaking of Java 16, this list is all but complete. I assume the oracle tutorials are not updated on a regular basis. Your better bet is to check the JLS
    – Qw3ry
    Jul 7, 2021 at 13:59
50
Java Escape Sequences:

\u{0000-FFFF}  /* Unicode [Basic Multilingual Plane only, see below] hex value 
                  does not handle unicode values higher than 0xFFFF (65535),
                  the high surrogate has to be separate: \uD852\uDF62
                  Four hex characters only (no variable width) */
\b             /* \u0008: backspace (BS) */
\t             /* \u0009: horizontal tab (HT) */
\n             /* \u000a: linefeed (LF) */
\f             /* \u000c: form feed (FF) */
\r             /* \u000d: carriage return (CR) */
\"             /* \u0022: double quote (") */
\'             /* \u0027: single quote (') */
\\             /* \u005c: backslash (\) */
\{0-377}       /* \u0000 to \u00ff: from octal value 
                  1 to 3 octal digits (variable width) */

The Basic Multilingual Plane is the unicode values from 0x0000 - 0xFFFF (0 - 65535). Additional planes can only be specified in Java by multiple characters: the egyptian heiroglyph A054 (laying down dude) is U+1303F / 𓀿 and would have to be broken into "\uD80C\uDC3F" (UTF-16) for Java strings. Some other languages support higher planes with "\U0001303F".

3
  • The existing answer does not address unicode and octal escape sequences in Java.
    – Ehryk
    Mar 15, 2015 at 18:29
  • 3
    \u000a does not seem to work -> - invalid character constant see more here
    – Jan
    Mar 26, 2015 at 11:02
  • 7
    @Jan It is working, perhaps too well. Unlike, for example, \r and \n, unicode escapes are pre-processed before the compiler is run as the question you linked to specifies. As such, it's inserting a literal line feed into your code and failing because of it. However, the escape code is "working" as it was intended to work in the specification.
    – Ehryk
    Feb 16, 2016 at 18:26
1

Well additionally, older versions of Java supported \v for form feed.

From: java/util/regex/Pattern.java:

2600         case 'v':
2601             // '\v' was implemented as VT/0x0B in releases < 1.8 (though
2602             // undocumented). In JDK8 '\v' is specified as a predefined
2603             // character class for all vertical whitespace characters.
2604             // So [-1, root=VertWS node] pair is returned (instead of a
2605             // single 0x0B). This breaks the range if '\v' is used as
2606             // the start or end value, such as [\v-...] or [...-\v], in
2607             // which a single definite value (0x0B) is expected. For
2608             // compatibility concern '\013'/0x0B is returned if
0

Yes, below is a link of docs.Oracle where you can find complete list of escape characters in Java.

Escape characters are always preceded with "\" and used to perform some specific task like go to next line etc.

For more Details on Escape Character Refer following link:

https://docs.oracle.com/javase/tutorial/java/data/characters.html

0
0

These are escape characters which are used to manipulate string.

\t  Insert a tab in the text at this point.
\b  Insert a backspace in the text at this point.
\n  Insert a newline in the text at this point.
\r  Insert a carriage return in the text at this point.
\f  Insert a form feed in the text at this point.
\'  Insert a single quote character in the text at this point.
\"  Insert a double quote character in the text at this point.
\\  Insert a backslash character in the text at this point.

Read more about them from here.

http://docs.oracle.com/javase/tutorial/java/data/characters.html

Not the answer you're looking for? Browse other questions tagged or ask your own question.