Use apache commons StringEscapeUtils.
System.out.println("String s = \""
+ StringEscapeUtils.escapeJava(string)
+ "\";");
A tab character "\t"
is then replaced with a backslash and a t
. As others said, the String representations represents some special characters like linefeed/newline with \n
.
The above would be fit for generating Java source code or so.