Zwedgy
Zwedgy

Reputation: 330

Check if string contains all strings from array

How to check if String contains all Strings from Array.

My current code:

String word = "abc";
String[] keywords = {"a", "d"};

for(int i = 0; i < keywords.length; i++){
    if(word.contains(keywords[i])){
       System.out.println("Yes");
    }else{
       System.out.println("No");   
    }
}

Upvotes: 6

Views: 17665

Answers (7)

Andremoniy
Andremoniy

Reputation: 34900

The code would look much more nicer if you wrap it into a separate method:

public static boolean containsAllWords(String word, String ...keywords) {
    for (String k : keywords)
        if (!word.contains(k)) return false;
    return true;
}

Upvotes: 12

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59950

Note this solution work only if your keywords contain one char, there are many answers already mentioned if your keywords contain more then one char.

With one line :

boolean contain = Arrays.asList(word.split("")).containsAll(Arrays.asList(keywords));

The idea is :

String word = "abc";
String[] split = word.split("");

Split your String to get an array of chars split = {a, b, c}

String[] keywords = {"a", "b"};

Check if your array1 contain all the element of the second array2 using containsAll

boolean contain = Arrays.asList(split).containsAll(Arrays.asList(keywords));

Upvotes: 1

Pardeep
Pardeep

Reputation: 1005

Either use StringUtils (org.apache.commons.lang.StringUtils). It will return the index of the first occurrence of keywords or -1 if it is not there.

StringUtils.indexOfAny(word, keywords);

Or you can use Arrays which will return the boolean value.

Arrays.asList(word).contains(keywords)

Upvotes: -1

Oneiros
Oneiros

Reputation: 4378

Use a boolean variable that will tell you if every keyword is matched. Set it to true as default value. Then check every keword: if any one is not contained in your word, stop searching and set your variable to false.

boolean containsAll = true; 
for (String keyword : keywords){
    if (!word.contains(keyword)){
       containsAll = false;
       break;
    }
}

Upvotes: 5

Aurasphere
Aurasphere

Reputation: 4011

Simply use a counter:

String word = "abc";
String[] keywords = {"a", "d"};

int counter = 0;

for(int i = 0; i < keywords.length; i++){
    if(word.contains(keywords[i])){
       counter++;
    }
}

if(counter == keywords.length){
   System.out.println("Yes");
}else{
   System.out.println("No");   
}

If the counter equals the keywords length, it means that all elements are contained. With this solution you will also be able to find out how many keywords are matched by the word.

Upvotes: -2

Elliott Frisch
Elliott Frisch

Reputation: 201429

If you are using Java 8+, you could use a Stream and test if all of the elements match your criteria with one line. Like,

if (Stream.of(keywords).allMatch(word::contains)) {
    System.out.println("Yes");
} else {
    System.out.println("No");
}

In earlier versions, or if you want to understand what the above is doing, it might look something like

boolean allMatch = true;
for (String kw : keywords) {  // <-- for each kw in keywords
    if (!word.contains(kw)) { // <-- if "word" doesn't contain kw
        allMatch = false;     // <-- set allMatch to false
        break;                // <-- stop checking
    }
}
if (allMatch) {
    System.out.println("Yes");
} else {
    System.out.println("No");
}

Upvotes: 9

Deepesh Choudhary
Deepesh Choudhary

Reputation: 677

A better code would be:

for(String s: keywords){
    if(word.contains(s)){
       System.out.println("Yes");
    }else{
       System.out.println("No");   
    }
}

Upvotes: -2

Related Questions