A
A
askalidov2022-04-06 18:10:52
Java
askalidov, 2022-04-06 18:10:52

How to determine if an array of strings is contained within another array of strings?

There is a first array of strings: And a second one: all elements of st2 are in st1
String[] st1 = {"I", "am", "Bob"};
String[] st2 = {"I", "Bo"};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
askalidov, 2022-04-06
@askalidov

private static boolean containsAll(String[] substr, String[] str) {
        for (String s : substr) {
            if (!Arrays.toString(str).contains(s)) {
                return false;
            }
        }
        return true;
    }

D
Dmitry Roo, 2022-04-06
@xez

You need to go through each element from the second array and find out if it is contained in the first array.
If all are contained, return true, if at least one was not found, return false.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question