M
Muhammad Ali
Hi,
I have string
String s = "Hello \"World\"";
when i print this on console, it prints
Hello "World"
It works fine. Now what i want to is to print
Hello \"World\"
Which means i have to replace the quotes (") with (\\) and then a
quote so that i may get a quote in output. so i used replaceAll method
System.out.println(s.replaceAll("\"", "\\\""));
But to my astonish, it prints out
Hello "World".
I wondered and then tried to do it again with
System.out.println(s.replaceAll("\"", "\\\\\""));
Now i was able to see the desired outout i.e; Hello \"World\"
But why i have to use 5 back slashes instead of 3 which were according
to the logic of "backsequences"?? I have seen the replaceAll method
and it is using the pattern matching in it.
If i try to replace the string using simple "replace" method it works
fine; i.e i don't have to use 5 backslashes, i use only 3 (according
to logic). Can someone explain why i have to use 5 slasheds in
replaceAll.
I have string
String s = "Hello \"World\"";
when i print this on console, it prints
Hello "World"
It works fine. Now what i want to is to print
Hello \"World\"
Which means i have to replace the quotes (") with (\\) and then a
quote so that i may get a quote in output. so i used replaceAll method
System.out.println(s.replaceAll("\"", "\\\""));
But to my astonish, it prints out
Hello "World".
I wondered and then tried to do it again with
System.out.println(s.replaceAll("\"", "\\\\\""));
Now i was able to see the desired outout i.e; Hello \"World\"
But why i have to use 5 back slashes instead of 3 which were according
to the logic of "backsequences"?? I have seen the replaceAll method
and it is using the pattern matching in it.
If i try to replace the string using simple "replace" method it works
fine; i.e i don't have to use 5 backslashes, i use only 3 (according
to logic). Can someone explain why i have to use 5 slasheds in
replaceAll.