6

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");   
    }
}
1
  • So what's wrong with your current code? Commented Apr 14, 2017 at 13:34

7 Answers 7

12

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;
}
Sign up to request clarification or add additional context in comments.

Comments

9

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");
}

Comments

5

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;
    }
}

1 Comment

I'd prefer this over having multiple return statements. Small changes though, boolean containsAll; for (String keyword : keywords){ containsAll = word.contains(keyword); if (!containsAll){ break; } }
1

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));

3 Comments

@CoronA but the OP's example contain one letter and not two like you do
@JCF: Algorithms usually do not solve examples but problems.
@CoronA i think that note now, fix every thing no? check my edit :)
-1

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)

1 Comment

There is a method StringUtils::containsAny, however OP needs the check against all search strings. Arrays.asList(word) will return a list with only the single element word. The method Collections::contains can not receive an array. Though there is the method Collections::containsAll. This doesn't work.
-2

A better code would be:

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

2 Comments

You forget the break statement.
before commenting please make sure your code is correct and also care to explain your.
-2

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.

1 Comment

I don't get what you mean sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.