How to replace \\ n with \ n in Java

I have string test="first \\n middle \\n last"

Now I want to replace everything "\\n"with"\n"

I tried test.replaceAll("\\\\n", "\\n")and test.replaceAll("\\n", "\n")but they do not work Does anyone have a solution?

Thank!

+3
source share
1 answer

Use this code:

String test="first \\n middle \\n last";
System.out.println("Output: " + test.replaceAll("\\\\n", "\n"));

OUTPUT

Output: first 
 middle 
 last

"\\\\"+ "n"for backslash "\\"and "n"in the source line is replaced by"\n"

+8
source

All Articles