Not able to replace all for dollar sign

It is special character you need to use escape character

Try with this \\$

and it doesn’t make sense in your code you are trying to replacing the content with 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 $$

Leave a Comment