Recipe 3.11 Entering Nonprintable Characters


You need to put nonprintable characters into strings.


Use the backslash character and one of the Java string escapes.


The Java string escapes are listed in Table 3-1.

Table 3-1. String escapes

To get:






Linefeed (Unix newline)


See System.getProperty("line.separator"), which gives you the platform's line end.

Carriage return



Form feed






Single quote



Double quote



Unicode character


Four hexadecimal digits (no \x as in C/C++). See for codes.

Octal(!) character


Who uses octal (base 8) these days?




Here is a code example that shows most of these in action:

// System.out.println("Java Strings in action:"); // System.out.println("An alarm or alert: \a");    // not supported System.out.println("An alarm entered in Octal: \007"); System.out.println("A tab key: \t(what comes after)"); System.out.println("A newline: \n(what comes after)"); System.out.println("A Unicode character: \u0207"); System.out.println("A backslash character: \\");

If you have a lot of non-ASCII characters to enter, you may wish to consider using Java's input methods, discussed briefly in the JDK online documentation.

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: