Unable to replace everything with a dollar sign

can anyone tell me why I came across an exception exception index when running this method to replace the value with $sign?

eg. i am sending a message$$vmdomodm$$

message = message.replaceAll("$", "$");

I tried to see this forum, but could not understand the content

http://www.coderanch.com/t/383666/java/java/String-replaceAll

+5
source share
2 answers

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 $$
+27
source

- , replaceAll String # replace, :

message = message.replace("$", "$");
+3

All Articles