0

Java String.replace not working even after assigning to a variable.

I have a JSON formatted string like as follows.

{"Obligations":["\nAggregate","\nDelay"],"Result":"Permit"}

I want to remove the \n from the string.

I used the following piece of code. There is no difference.

    String test =jsonObject.toString().replace("\n", ""); 
    System.err.println(test);

But I get the same result. Is there something wrong in it ?

jgm
  • 1,212
  • 1
  • 18
  • 39

2 Answers2

2

"\n" means a new line. You must use an escape.

Try it:

String test =jsonObject.toString().replace("\\n", ""); 
LucasDelboni
  • 119
  • 1
  • 4
0

This works.

String test =jsonObject.toString().replace("\\n", ""); 
System.err.println(test);
jgm
  • 1,212
  • 1
  • 18
  • 39