0

I'm trying to compare two strings with one another. One contains for example :

String x =" aa bb cc dd ee " 

and the other one :

String y =" aa bb "

Now I want to do this

if (any values from String x EXISTS IN String y) 
if (y.matches(???)){System.out.println("true");}
else{System.out.println("false");}

I know that I can to do the opposite by using regex, like this :

if (x.matches(".*"+y+".*")){do something}

But I'd like to know if there is any way to check if any value in String x matches String y.

asked Jun 23, 2012 at 17:56
1
  • Should your code return true if x = "aa bb cc dd ee" and y = ee aa? Commented Jun 23, 2012 at 18:02

3 Answers 3

2

Assuming that what you call "a value in a String" is a pair of characters between spaces, split both Strings to transform them in sets of values (Set<String>), and use the Set methods to compute what you want (containsAll(), removeAll(), retainAll(), etc.).

answered Jun 23, 2012 at 18:01
Sign up to request clarification or add additional context in comments.

3 Comments

No, that was just an example I'm reading data from a file and it can't simply split
Then explain what you call a "value". But I don't see how you could not constitute a set of words from a file.
I'm still thinking that you should just have two sets, and compare those sets. The only difference is that you should use Date to represent a Date, and not String. BTW, you just accepted an answer that does exactly what I suggest to do in my answer (but in a suboptimal way).
1

If you are trying to see if String x is in String y somewhere then use the .contains(String str) method.

If you are trying to determine if the white space delimited parts of the strings are contained within y then you will need to split and then do contains on each part of the split operation.

answered Jun 23, 2012 at 18:02

2 Comments

.contain doesn't work because there are values in sting x that do not exist in the other one.
That would be why I suggested splitting x into whitespace delimited parts and then using contains on each of those parts. Much as JB Nizet suggested in an answer below.
1
String x_trimmed = x.trim();
String y_trimmed = y.trim();
String[] x_elements = x_trimmed.split("\\s+");
String[] y_elements = y_trimmed.split("\\s+");
Set<String> x_elementsSet = new HashSet<String>(Arrays.asList(x_elements));
Set<String> y_elementsSet = new HashSet<String>(Arrays.asList(y_elements));
for (String x : x_elementsSet) {
 if (y_elementsSet.contains(x)) {
 System.out.println("true");
 } else {
 System.out.println("false");
 }
}
answered Jun 23, 2012 at 18:07

3 Comments

Why not use String.trim() to trim? Why use List instead of Set? Why transform the array to a List at each iteration?
@JBNizet: Right! Thanks a lot for the hint! :)
Just because you name a variable xxxSet doesn't make it a Set. Use java.util.HashSet instead of a java.util.List. It's much more efficient at lookups (O(1) rather than O(n)).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.