Suhail Gupta
Suhail Gupta

Reputation: 23276

Is there a way I can check multiple condition with one 'matches' function?

Consider the following statement :

if(UserAgent.matches("iPhone"))

I have to detect whether the user is online from a mobile or a PC. To do this I need to check for large number mobile devices,browsers. Instead of writing the same part :

if(UserAgent.matches())

Is there any way I can check this using on matches function ? I meant to say by using | which is OR. Like :

UserAgent.matches(SOME MODIFICATION "iPhone" | "Android" | "Blackberry".....)

Upvotes: 0

Views: 150

Answers (2)

Håvard Geithus
Håvard Geithus

Reputation: 5624

Put all strings in a Set, then check if the UserAgent string is contained in that set.

if set.contains(UserAgent.toString())
    // do something
end

Upvotes: 0

Raymond Tau
Raymond Tau

Reputation: 3479

Actually, String.matches() is matching as a Regular Expression, so you may write something like

if (UserAgent.matches("iPhone|Android|Blackberry"))

Upvotes: 3

Related Questions