1

I am reading a string of text from a source that I cannot see otherwise. I would like to see the actual formatting codes such as: \t \n etc. instead of actual tabs and newline

When I print the String using System.out.println, I see the tabs visually and would have to count them by highlight with the mouse.

I've tried using a regex to replace the \t in the string with a \\t but I have little experience with regex so I'm not sure if my expression is correct. Here's what I have:

String text = extractor.getText();
text.replaceAll("\n", "\\n");

I would appreciate an alternate method of doing this without regex if possible. I'm using an XSSFExcelExtractor if it makes any difference.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Ron
  • 1,450
  • 15
  • 27
  • I realize that the formatting codes like '\n' are a single character. I'm guessing I can't match just the \ – Ron Oct 07 '13 at 03:27

3 Answers3

1

You can try using StringEscapeUtils from apache commons if you want to see all characters that Java will convert

String s="\r\t\n abc";
System.out.println(StringEscapeUtils.escapeJava(s));

output

\r\t\n abc

In case you want to escape only \t or \n you can use

String escaped = origina.replace("\t","\\t").replace("\n","\\n)"`
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • I like this, it's only for testing so I don't mind using apache commons. Still it would be nice to find a simpler way to do this. I'm going to leave the question open to see if someone suggests a built-in technique – Ron Oct 07 '13 at 03:36
  • Yeah that works, I saw CharSequence and for some reason thought it wouldn't accept a plain string, should have used `replace()` instead of `replaceAll()`. Secondly. if `escapeJava(String)` had to be built into commons I'll assume there's no duplicated functionality in the Java API, making your answer as simple as possible. – Ron Oct 07 '13 at 03:41
0
String s="\r\t\n abc";
System.out.println(StringEscapeUtils.escapeJava(s));
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
-1

Try:

text.replaceAll("\\\", "/");
Harry
  • 87,580
  • 25
  • 202
  • 214
yangsibai
  • 1,637
  • 12
  • 23
  • This throws a java.util.regex.PatternSyntaxException. I belive that a single backslash is found with the regex "\\\\" http://stackoverflow.com/questions/4025482/cant-escape-the-backslash-with-regex/4025505#4025505 – Ron Oct 07 '13 at 03:31
  • 1
    If it is a suggestion, please leave it in the comments section. If you are really answering, please follow SO formatting. – thefourtheye Oct 07 '13 at 03:51