2

I have one String "1,3,5,7,9,11,12,14" and I want to check whether the string contains "12,3,14," or not in java.

My code:

String str1 = "1,3,5,7,9,11,12,14";
String str2 = "12,3,14,";

if(str1.contains(str2)){
                System.out.println("Found!");
}
4
  • you already know it. Then whats the question ? Commented Sep 4, 2016 at 8:29
  • @MSach - In str2, the numbers are not in order Commented Sep 4, 2016 at 8:29
  • 1
    You have to use split(), split the string and then check if each string in str2's array is present in str1's array Commented Sep 4, 2016 at 8:30
  • @TheLostMind thanks then question is not clear. With your clarification i explained what he needs to do. Commented Sep 4, 2016 at 8:33

4 Answers 4

4

I think the question that you want to ask here is:

How to check if an array consisting of String values separated by a comma(,) consists of all the values of another array that are also separated by a comma(,).

If that's the case, then you can't use contains() method because that serves a different purpose.

Just define your own method (for example: containsValues() here). And define the suitable logic:

import java.util.*;

public class HelloWorld {
    public static void main(String[] args) {
        String str1 = "1,3,5,7,9,11,12,14";
        String str2 = "12,3,14,";
        if (containsValues(str1, str2)) {
            System.out.println("Found!");
        }
    }

    public static boolean containsValues(String str1, String str2) {
        String strArr1[] = str1.split(",");
        String strArr2[] = str2.split(",");
        return Arrays.asList(strArr1).containsAll(Arrays.asList(strArr2));
    }
}

Output

Found!

Sign up to request clarification or add additional context in comments.

2 Comments

"12,3,14,".split(",") results array [12, 3, 14, <empty>], so containsAll will return false.
No @SashaSalauyou. I think you are mistaken. split() doesn't create that empty slot. It only returns [12, 3, 14]. You can do a simple System.out.println("12,3,14,".split(",").length); to verify it. You'll get the output as 3.
1

Don't see any problem in your code. You are not getting Found!, because str1 actually does not contain "12,3,14,".

    String str1 = "1,3,5,7,9,11,12,14";
    String str2 = "12,3,14,";
    String str3 = "1,3,5";

    if (str1.contains(str2)) { //false
        System.out.println("Found!");
    }

    if (str1.contains(str3)) { //true
        System.out.println("Found!");
    }

But if you want to find them individually in str1, you may split your str2 and find them one by one.

    String str1 = "1,3,5,7,9,11,12,14";
    String str2 = "12,3,14,";       
    String s[] = str2.split(",");

    for (String str : s) {
        if(str != "" && str1.contains(str))
            System.out.println(str+ " found!");
    }

Comments

0

Try something like this:

String stack = "1,3,5,7,9,11,12,14";
String needle = "12,3,14,";

return Arrays.asList(stack.split(","))
       .containsAll(Arrays.asList(needle.split(",", 0)));

Notice usage of 0 in String#split() to discard empty trailing substring in malformed needle.

Comments

-1

explore String java docs . There is split() method to do this

  1. split the second string with delimiter as comma
  2. Then check if every element is contained in source or not

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.