Reputation: 416
I'm trying to search for a substring in a string and replace it. I'm using
String p= "+ 0.0";
But this line gives me a dangling metacharacter error. I've tried typecasting this but it still doesn't work. How do I fix this?
I want to do the following
if(s.containts(p)){ //s is a given string
s.replaceAll(p,"");
}
On a related note,
s.containts("+ 0.0"))
throws no dangling metacharater error but
s.replaceAll("+ 0.0","");
throws the error.
Is there a reason for this?
Upvotes: -3
Views: 626
Reputation: 17805
Escape the +
and .
, since +
and .
are meta characters of a regular expression.
String p= "\\+ 0\\.0";
Upvotes: -1