Check if String has '+' character

4

I need to look up a string if it has a + character, like this:

teste = teste.replaceAll("+", "e");

but I get this error:

  

java.util.regex.PatternsSyntaxException: Dangling meta character '+'

    
asked by anonymous 02.06.2017 / 05:25

1 answer

5

The + character is reserved in regular expressions, so it is necessary to escape it, however in Java the \ character is used to escape expressions within String , so there is a need to use it twice to ensure literal use. Replacing in your expression would look like this:

teste = teste.replaceAll("\+", "e");

The% w of% after a string of characters means "one or more occurrences", for example: + which means "one or more occurrences of a number (from zero to nine)."     

02.06.2017 / 13:43