I have to display string with visible control characters like \n
, \t
etc.
I have tried quotations like here, also I have tried to do something like
Pattern pattern = Pattern.compile("\\p{Cntrl}");
Matcher matcher = pattern.matcher(str);
String controlChar = matcher.group();
String replace = "\\" + controlChar;
result = result.replace(controlChar, replace);
but I have failed
just doing
will work!!
Alternative: Use visible characters instead of escape sequences.
To make control characters "visible", use the characters from the Unicode Control Pictures Block, i.e. map
\u0000
-\u001F
to\u2400
-\u241F
, and\u007F
to\u2421
.Note that this requires output to be Unicode, e.g. UTF-8, not a single-byte code page like ISO-8859-1.
Output using method above as input text:
Simply replace occurences of
'\n'
with the escaped version (i.e.'\\n'
), like this:For example:
Will yield the output: