This is a special character that you need to use an escape character
Try with this \\$
and it makes no sense in your code that you are trying to replace the contents with the same
String message = "$$hello world $$";
message = message.replaceAll("\\$", "_");
System.out.println(message);
Output
__hello world __
Update
String message = "$hello world $$";
message = message.replaceAll("$", "\\$");
System.out.println(message);
Output
$hello world $$
source
share