I’m looking to search for 2 or 3 key phrases in a string called lastTestText and I want to return a Boolean. It works nicely with:
Boolean tooLong = lastTestText.contains("hour") | lastTestText.contains("min");
But this is going to get a bit long-winded if I have 4 or 5 phrases so I wondered if there’s a more elegant way of doing this?
You could use a regular expression:
You could even check for plurals, using an expression like this:
".*(?>hour(?>s)?|min(?>s)?|second(?>s)?).*"which would matchhour, hours, min, mins, second, seconds.Note, however, that it would also match
millisecondssince it containsseconds. In that case the expression would become more complex because you need to use word boundaries:".*\\b(?>hour(?>s)?|min(?>s)?|second(?>s)?)\\b.*"