How do I test a string to see if it contains any of the strings from an array?
Instead of using
if (string.contains(item1) || string.contains(item2) || string.contains(item3))
How do I test a string to see if it contains any of the strings from an array?
Instead of using
if (string.contains(item1) || string.contains(item2) || string.contains(item3))
The easiest way would probably be to convert the array into a java.util.ArrayList. Once it is in an arraylist, you can easily leverage the contains method.
And if you are looking for case insensitive match, use pattern
Since version 3.4 Apache Common Lang 3 implement the containsAny method.
Try this:
String Utils
Use:
It will return the index of the string found or -1 if none is found.
If you use Java 8 or above, you can rely on the Stream API to do such thing:
Assuming that you have a big array of big
String
to test you could also launch the search in parallel by callingparallel()
, the code would then be: