Compare one String with multiple values in one expression

I found the better solution. This can be achieved through RegEx:

if (str.matches("val1|val2|val3")) {
     // remaining code
}

For case insensitive matching:

if (str.matches("(?i)val1|val2|val3")) {
     // remaining code
}

Leave a Comment