Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing "\\n" in string Java

I have the following piece of code:

String text = "Category:Bishopsq\\nxof La2on</target></link></sentence>\\n</paragraph><paragraph>".toString().replaceAll("(\n|\\n)", "").trim().replaceAll("\\<.*?>", " ").replaceAll("[^a-zA-Z]", " ").trim().replaceAll(" +", " ").toLowerCase();
System.out.println(text);

I get the following output:

category bishopsq nxof la on n

However, I want the following output:

category bishopsq xof la on

It is not correctly replacing all \\n. What am I doing wrong?

like image 471
AngryPanda Avatar asked May 14 '26 21:05

AngryPanda


1 Answers

you need to escape the sign \, like this:

replaceAll("(\\n|\\\\n)", "")
like image 150
gefei Avatar answered May 17 '26 12:05

gefei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!