Java Programming
String variables
Special characters
Within a string, you can use escape sequences to include certain types of special characters.
Common escape sequences
Sequence | Character |
---|---|
\n | New line |
\t | Tab |
\r | Return |
\" | Quotation mark |
\\ | Backslash |
Example 1: New line
String"Code: JSPS\nPrice: $49.50"ResultCode: JSPS
Price: $49.50
Example 2: Tabs and returns
String"Joe\tSmith\rKate\tLewis"ResultJoe Smith
Kate Lewis
Example 3: Quotation marks
String"Type \"x\" to exit"ResultType "x" to exit
Example 4: Backslash
String"C:\\java\\files"ResultC:\java\files