0

I have one string that contains String s1="1 2 7 8"

now suppose I have two string String s2="1 7 8" and String s3="1 2 8"

so by comparing that two string to my first one it should return true.

But when I do s1.contanis(s2) it returns false.

Is there any other easy way to compare these type of patterns, without split each one and comparing one by one?

Senthil
  • 1,244
  • 10
  • 25
commit
  • 4,777
  • 15
  • 43
  • 70
  • it's [similar_text](http://php.net/similar_text) in php [ [check this out](http://ideone.com/A8DWrI) ]... same question [here as well](http://stackoverflow.com/a/955122/1273830). – Prasanth Jun 21 '13 at 08:42

7 Answers7

5

This will do the job of checking if s1 contains all the elements from s2 (separated by a space):

Arrays.asList(s1.split(" ")).containsAll(Arrays.asList(s2.split(" ")));

Complete code:

public class StringContainsNumbers {
    public static void main(String[] args) {
        final String s1 = "1 2 7 8";
        final String s2 = "1 7 8";
        final String s3 = "1 2 8";

        System.out.println(stringContainsNumbers(s1, s2));
        System.out.println(stringContainsNumbers(s1, s3));
    }

    private static boolean stringContainsNumbers(String s1, String s2) {
        return Arrays.asList(s1.split(" ")).containsAll(Arrays.asList(s2.split(" ")));
    }
}
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
1

s1 definitely does not contain s2.

what are you trying to do ? If you need to compare actual values, split and convert to integers

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 1
    he understands that it returns false. the question here is how to do that. that to he wants to know if there is any library that supports this. He knows that it can be done through iterating the string. – ajay.patel Jun 21 '13 at 08:46
1

Contains checks for the whole "1 7 8" as string in "1 2 7 8"So it returns false.

You should use StringTokenizer Method.

foobar
  • 2,887
  • 2
  • 30
  • 55
0

Because when you use s1.contains(s2), it does not compare single letters in the string but the entire string. For example, if the value of s1 is "2 1 7 8" then s1.contains(s2) will return true.

Eugene Yu
  • 3,708
  • 4
  • 21
  • 27
0

you can do google search "Java - String matches() Method" You are going to find many links. One of them is: http://www.tutorialspoint.com/java/java_string_matches.htm

Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
0

Use following code:

public class Main {
 public static void main(String[] args) {
    String one="1 7 8";
    String two= "1 2 8";
    List<String> fistList= Arrays.asList(one.split(" "));
    List<String> secondList= Arrays.asList(two.split(" "));
    boolean isContainAll=fistList.containsAll(secondList);
    System.out.println(isContainAll);
 }
}
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

The contains method on String only returns true if s1 completely contains s2. In other words

 String s1 = "hello world";
 s1.contains( "llo" );

Would return true but

 s1.contains( "ll w" );

would return false.

You'll need to tokenize both s1 and s2 and then check that the s1 elements contain all the s2 elements.

wobblycogs
  • 4,083
  • 7
  • 37
  • 48