Check if a row contains any of the rows from the array

How to check a string to see if it contains any rows from an array?

Instead of using

if (string.contains(item1) || string.contains(item2) || string.contains(item3)) 
+79
java string coding-style if-statement
Jan 24 '12 at 18:32
source share
12 answers

EDIT: This is an update using the Java 8 Streaming API. So much cleaner. Can also be combined with regular expressions.

 public static boolean stringContainsItemFromList(String inputStr, String[] items) { return Arrays.stream(items).parallel().anyMatch(inputStr::contains); } 

In addition, if we change the input type to a list instead of an array, we can use items.parallelStream().anyMatch(inputStr::contains) .

You can also use .filter(inputStr::contains).findAny() if you want to return the corresponding string.




Original slightly dated answer:

Here is a static method (VERY BASIC). Note that it is case sensitive in comparison strings. A primitive way to make it case insensitive would be to toLowerCase() or toUpperCase() on the input and test lines.

If you need to do something more complex, I would recommend looking at Pattern and Matcher and learning how to perform some regular expressions. String.matches() you understand them, you can use these classes or the helper method String.matches() .

 public static boolean stringContainsItemFromList(String inputStr, String[] items) { for(int i =0; i < items.length; i++) { if(inputStr.contains(items[i])) { return true; } } return false; } 
+86
Jan 24 2018-12-12T00:
source share
 import org.apache.commons.lang.StringUtils; 

String Utils

Using:

 StringUtils.indexOfAny(inputString, new String[]{item1, item2, item3}) 

It will return the index of the found row, or -1 if none are found.

+37
May 28 '15 at 1:17
source share

You can use the String # method as follows:

 System.out.printf("Matches - [%s]%n", string.matches("^.*?(item1|item2|item3).*$")); 
+26
Jan 24 '12 at 18:38
source share

The easiest way is to convert the array to java.util.ArrayList. When it is in the archaist, you can easily use the contains method.

 public static boolean bagOfWords(String str) { String[] words = {"word1", "word2", "word3", "word4", "word5"}; return (Arrays.asList(words).contains(str)); } 
+18
Jan 24 '12 at 18:36
source share

Try the following:

 if (Arrays.asList(item1, item2, item3).contains(string)) 
+7
Jan 24 '12 at 18:37
source share

If you use Java 8 or higher, you can rely on the Stream API to do this thing:

 public static boolean containsItemFromArray(String inputString, String[] items) { // Convert the array of String items as a Stream // For each element of the Stream call inputString.contains(element) // If you have any match returns true, false otherwise return Arrays.stream(items).anyMatch(inputString::contains); } 

Assuming you have a large array of String to test, you can also parallelize the search by calling parallel() , the code would be this:

 return Arrays.stream(items).parallel().anyMatch(inputString::contains); 
+7
Aug 31 '16 at 7:42
source share

Here is one solution:

 public static boolean containsAny(String str, String[] words) { boolean bResult=false; // will be set, if any of the words are found //String[] words = {"word1", "word2", "word3", "word4", "word5"}; List<String> list = Arrays.asList(words); for (String word: list ) { boolean bFound = str.contains(word); if (bFound) {bResult=bFound; break;} } return bResult; } 
+2
Jan 6 '16 at 11:10
source share
 if (Arrays.asList(array).contains(string)) 
+1
Jan 24 '12 at 18:37
source share

A more serious approach would be to use insertion in combination with metaClass :

I would like to say:

 String myInput="This string is FORBIDDEN" myInput.containsAny(["FORBIDDEN","NOT_ALLOWED"]) //=>true 

And this method will be:

 myInput.metaClass.containsAny={List<String> notAllowedTerms-> notAllowedTerms?.inject(false,{found,term->found || delegate.contains(term)}) } 

If you need containsAny for any future string variable, add a method to the class instead of an object:

 String.metaClass.containsAny={notAllowedTerms-> notAllowedTerms?.inject(false,{found,term->found || delegate.contains(term)}) } 
0
Nov 18 '13 at 10:44
source share

And if you are looking for case insensitive use the pattern

 Pattern pattern = Pattern.compile("\\bitem1 |item2\\b",java.util.regex.Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(input); if(matcher.find() ){ } 
0
Feb 03 '17 at 14:17
source share
 String.prototype.matchWithTokens = function(tokensArray){ for(i=0; i< tokensArray.length; i++) { if(this.indexOf(tokensArray[i]) != -1) { return true; } } return false; }; 
-one
Apr 26 '16 at 18:53
source share

The following should work for you, assuming Strings is the array you are looking for inside:

 Arrays.binarySearch(Strings,"mykeytosearch",mysearchComparator); 

where mykeytosearch is the string you want to check for existence within the array. mysearchComparator is a comparator that will be used to compare strings.

See Arrays.binarySearch for details .

-2
Jan 24 '12 at 18:43
source share