0

I am having an xml

<field>
       <fieldSeparator>\t</fieldSparator>
       <fieldOrder>field1,field2,field3</fieldOrder>
</field>

corresponsing FieldVO has the respective values in it.

StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("someValue").append(fieldSeparator).append("SomeOtherText");

Output - someValue\tSomeOtherText

Here i want to render the tab space instead of "\t".

But

String str = "text1,text2,text3";
String str1="\t";
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(str.replaceAll(",",str1));
System.out.println(stringBuffer);

Output --> text1 text2 text3.

Can anybody explain those 2 behaviours?

Thanks.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • Possible duplicate of [Howto unescape a Java string literal in Java](http://stackoverflow.com/questions/3537706/howto-unescape-a-java-string-literal-in-java/4298836)? – Balder Feb 19 '14 at 11:05

3 Answers3

0

Check your string at runtime (through a debugger).
It probably has \\t and not \t.
If so, the behavior is quite normal.
You should see what exactly you have in your string.
Then you should replace \\t with \t.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

When an XML file is read, then \t is read as two separate characters. In Java it will look like this: \\t. Just replace it like:

str.replace("\\t", "\t");
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
0

You have to use \\t instead of \t.

D3X
  • 547
  • 3
  • 20