Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java replaceAll with newline symbol

the newline symbol \n is causing me a bit of trouble when i try to detect and replace it: This works fine:

String x = "Bob was a bob \\n";
String y = x.replaceAll("was", "bob");
System.out.println(y);

butt this code does not give the desired result

String x = "Bob was a bob \\n";
String y = x.replaceAll("\n", "bob");
System.out.println(y);
like image 426
Kay Avatar asked Jan 21 '23 21:01

Kay


1 Answers

"Bob was a bob \\n" becomes literally Bob was a bob \n

There is no newline to replace in the input string. Are you trying to replace a newline character or the escape sequence \\n?

like image 53
Dark Falcon Avatar answered Jan 30 '23 18:01

Dark Falcon