Java String Escape
Escape special characters in Java strings, including Unicode escape sequences
About Java String Escaping
Java strings use backslash as the escape character. Unlike other languages, Java's Unicode escapes (\uXXXX) are processed in the earliest phase of compilation, meaning they can appear anywhere in the code, not just in strings.
Proper string escaping is crucial for handling internationalized text, special characters, and cross-platform compatibility. Java supports standard escape sequences as well as Unicode escapes that can represent any Unicode character.
Unicode Escape Features
Java's Unicode escapes (\uXXXX) have some unique characteristics:
- Unicode escapes are processed during lexical analysis, before other escapes
- Unicode escapes can be used in comments and identifiers
- \u must be followed by exactly 4 hexadecimal digits
- Multiple u's can be used, like \uuuuXXXX (for escaping itself)
Escape Rules Reference
| Escape Sequence | Unicode | Description |
|---|---|---|
| \\ | U+005C | Backslash |
| \' | U+0027 | Single quote |
| \" | U+0022 | Double quote |
| \n | U+000A | Line Feed |
| \r | U+000D | Carriage Return |
| \t | U+0009 | Tab |
| \b | U+0008 | Backspace |
| \f | U+000C | Form Feed |
| \uXXXX | U+XXXX | Unicode character (4-digit hex) |
Common Use Cases
Internationalization (i18n)
Represent non-ASCII characters in properties files and resource bundles.
JSON/XML Processing
Escape special characters when embedding JSON or XML strings in Java code.
Regular Expressions
Backslashes need to be double-escaped in Java regex.
Database Queries
Handle quotes in strings when building SQL statements.
Text Blocks (Java 13+)
Java 13 introduced Text Blocks, enclosed by three double quotes """, which can contain multiple lines of text without escaping newlines. Text blocks preserve formatting, automatically handle indentation, and are the modern way to handle long strings.
Usage Tips
- Use StringEscapeUtils (Apache Commons) for complex escape operations
- In Properties files, equals signs and colons also need escaping
- In regex, backslash needs to be written as \\\\ (four backslashes)
- Consider using text blocks to simplify multiline strings
- IDEs usually provide shortcuts for string escaping operations