How do I print the escaped representation of a string, for example if I have:
s = "String:\tA"
I wish to output:
String:\tA
on the screen instead of
String: A
I think you are looking for:
String xy = org.apache.commons.lang.StringEscapeUtils.escapeJava(yourString);
System.out.println(xy);
from Apache Commons Lang v2.6
deprecated in Apache Commons Lang v3.5+, use Apache Commons Text v1.2
Well strictly speaking the internal representation is an unsigned 16-bit integer. I think what you mean is that you want to escape the string.
There's a class called StringEscapeUtils in the Apache library to help with that.
String escaped = StringEscapeUtils.escapeJava("\t");
System.out.println(escaped); // prints \t
For a given String you'll have to replace the control characters (like tab):
System.out.println("String:\tA\n".replace("\t", "\\t").replace("\n","\\n");
(and for the others too)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With