Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to replace all for dollar sign

Tags:

java

can anyone advise why i encountered index out of bouns exception when running this method to replace the value by $ sign?

E.g. i pass in a message $$vmdomodm$$

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

I tried to look at this forum thread but couldnt understand the content

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

like image 488
Sillicon.Dragons Avatar asked Mar 13 '12 07:03

Sillicon.Dragons


1 Answers

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 $$
like image 77
jmj Avatar answered Oct 05 '22 12:10

jmj