0

Given a string, I want to replace all double slashes with one slash, and then all instances of \n with -. For instance, if I have this:

\\n\\nff

It should be converted to:

\n\nff

And then to:

--ff

I wrote this code:

shippingAddress = shippingAddress.replaceAll("\\\\n", "\n");

For example:

public static void main(String[] args) {
    System.out.println("xx");
    String x = "enterchar17\\n\\n\\nenterchar17enterchar17\\n\\n\\n\\nenterchar17";
    System.out.println(x);
    x.replaceAll("\\n","\n");
    System.out.println(x);
}

This produces this output:

 xx
enterchar17\n\n\nenterchar17enterchar17\n\n\n\nenterchar17
enterchar17\n\n\nenterchar17enterchar17\n\n\n\nenterchar17

so it did not change. I need to escape escape char because somehow spring boot sends 2 slashes. What can i do?

i think JSON does think double as double. It doesn't get it escape?

"line1": "fasfsafasfsa\n\n\nff\n\tasas",

on postman, when i send request like this, \n is converted to "-"

but in postman when i send with double, it cantconvert.

when i inspect to the page where i send request of string, i see this

line1
:
"enterchar17↵↵v↵↵enterchar17↵↵↵enterchar17"

on chromium

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
mark
  • 727
  • 3
  • 14
  • 35

1 Answers1

2

I think you have java problem. The line

x.replaceAll("\\n","\n");

should be

x = x.replaceAll("\\n","\n");

String is an immutable class. Therefore, the output from a method needs to be captured in a variable.

Girum A
  • 63
  • 10