ChatGPT解决这个技术问题 Extra ChatGPT

What are all the escape characters?

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?

This is in the Java Language Spec
Don't ask questions on the Internet about things you can just as easily, or more easily, look up for yourself. You run the risk of grave error.
@user207421 though it is nice for the rest of us to find while googling.

B
Bjop

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.


The list is missing Unicode and octal escapes: \u1234 \012 \01 \0
\a does not compile in javac 1.8.0_20: illegal escape character: String test = "\a";
"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
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
E
Evgeniy Berezovsky
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".


The existing answer does not address unicode and octal escape sequences in Java.
\u000a does not seem to work -> - invalid character constant see more here
@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.
m
manisha mulchandani

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


A
Aleksandr Dubinsky

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


D
Douglas Held

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