I am trying below code but getting error
String x = \"aaa XXX bbb\"; String replace = \"XXX\"; String y = \"xy$z\"; String z=y.replaceAll(\"$\",
The reason for the error is that after the line:
String z=y.replaceAll("$", "\\$");
The value of z is: xy$z$ what you really want to do is:
z
xy$z$
String x = "aaa XXX bbb"; String replace = "XXX"; String y = "xy\\$z"; x = x.replaceFirst(replace, y); System.out.println(x);
which will output:
aaa xy$z bbb