0

I have to check, if String Array test2[] contains a value of test1[]. How to do that? Both Arrays have different size. I also have to check if test2[] contains a substring of test1[].

String[] test = {"Test1", "Test2"};
String[] test2 = {"Test3", "Test4", "Test5", "Test6", "Test1 - Test7"};
3
  • 2 foreach loops will do thee trick Commented Aug 18, 2018 at 17:35
  • 1
    "Contains" is a way too general here. Please clarify - should test2 contain test as a subset, or it should contain any value from test or it should contain a (continuous or not) subsequence of test Commented Aug 18, 2018 at 17:36
  • 2
    HashSet + contains would do the trick. Commented Aug 18, 2018 at 17:39

4 Answers 4

1

You just need couple nested loops and iterate. Your question is ambiguous and the contains word you say might mean equals or contains the substring. In either case, if you want equal match, just replace .contains() with .equals().

for (String value : test) {
        for (String sampleString : test2) {
            if (sampleString.contains(value)) {
                System.out.println("Value " + value + " is contained in the array in " + sampleString);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You could use two for loops, one to iterate test and the other to check if the item is contained in test2

public static boolean checkIfExists(String[] arr, String item) {
    for (String n : arr) {

         if (n.contains(item)) {
            return true;
         }
      }
      return false;
   }

And in main method

String[] test = {"Test1", "Test2"};
String[] test2 = {"Test3", "Test4", "Test5", "Test6", "Test1 - Test7"};
for(String t : test) {
    System.out.println(checkIfExists(test2, t));
}

Comments

0
public boolean checkIfHaveSameElements(String[] test1, String[] test2) {
    for (String str2 : test2) {
        for (String str1 : test1) {
            if (str2.equals(str1)) {
                return true;
            }
        }
    }
    return false;
}

Just pass your arrays as method call argumnents.

Comments

0

Using Java 8 APIs, I am first checking whether the entire string is present or whether the substring is present.

public class CheckString {

public static void main(String[] args) {      

    String[] test = {"Test1", "Test3", "Test2"};
    String[] test2 = {"Test3", "Test4", "Test5", "Test6", "Test1 - Test7"};

    boolean isPresent = Arrays.stream(test2)
                              .filter(str->{
                               return Arrays.asList(test).indexOf(str) > 0 || checkString(test,str);
                               })
                              .collect(Collectors.toList())
                              .isEmpty();

    System.out.println(!isPresent);

    }
    private static boolean checkString(String[] strs, String chkStr){
        for(String str: strs){
            if(chkStr.contains(str)){
                return true;
            }
        }
        return false;
    }
}

Comments

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.