Search code examples
javastringmetacharacters

Dangling metacharacter Java


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?


Solution

  • Escape the + and ., since + and . are meta characters of a regular expression.

    String p= "\\+ 0\\.0";