user3183679
user3183679

Reputation: 275

Can I compare against multiple strings with the equals() method?

Is it possible to get multiple strings with .equals?

if(something.equals("String1 String2 String3")){
   System.out.println(Something);
}

What I mean is:

if(choose.equals("DO IT")){
   sysout blah blah blah
}
else if(choose.equals("DONT DO IT")){
   ...
}

Upvotes: 3

Views: 5159

Answers (8)

Brian Agnew
Brian Agnew

Reputation: 272317

No, but an alternative for many strings is to put the strings in a collection and do something like:

Set<String> strings = new HashSet<>();
strings.add("A");
strings.add("B");
strings.add("C");

if (strings.contains("D")) {
  // ...
}

which is perhaps a little more concise. It's also null-safe wrt. the string you're looking to compare, which is often very useful.

Note further with Java 7 the switch statement works with strings, and that's useful if you wish to tie different actions to different strings.

Upvotes: 6

EpicPandaForce
EpicPandaForce

Reputation: 81549

Well, if you want to check if there are any in such a string that don't match (aka all must match, albeit that doesn't really seem to make sense to me), then

String initString = "String1 String2 String3";
String[] splitStrings = initString.split(" ");
boolean match = true;
for(String string : splitStrings)
{
    if(!string.equals(something))
    {
        match = false;
        break;
    }
}
if(match == true)
{
    //did match all of them
}
else
{
    //there was one that was not matched
}

If you want a "matches at least one" then it's just

String initString = "String1 String2 String3";
String[] splitStrings = initString.split(" ");
boolean match = false;
for(String string : splitStrings)
{
    if(string.equals(something))
    {
        match = true;
        break;
    }
}
if(match == true)
{
    //did match at least one of them
}
else
{
    //didn't match any of them
}

But to be honest, Java 8 makes this simpler:

String something = "whatever";
String initString = "String1 String2 String3";
String[] splitStrings = initString.split(" ");
boolean matchAll = Arrays.stream(splitStrings).allMatch((x) -> x.equals(something));
boolean matchAny = Arrays.stream(splitStrings).anyMatch((x) -> x.equals(something));

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476813

You can use a regex given the strings you match don't contain special regex characters, or are escaped.

Example:

Pattern p = Pattern.compile("^(String1|String2|String3)$");
if(p.matcher(something).find()) {
    //do something
}

Or you can store the strings in a set/list and query the set:

Example:

HashSet<String> possible = new HashSet<String>();
possible.add("String1");
possible.add("String2");
possible.add("String3");
if(possible.contains(Something)) {
    //do something
}

Upvotes: 1

SparkOn
SparkOn

Reputation: 8956

If you have gone through the javadocs it says

public boolean equals(Object obj); :

Indicates whether some other object is "equal to" this one.

It does not says that some other object is "equal to" these Objects.

Using equals() you can compare an Object with some other Object. It does not allow you to compare at once an Object with many other Objects. However if you want to compare an Object with many other Objects then you will need equals() for each comparasion

Upvotes: 0

Bohemian
Bohemian

Reputation: 425083

If you mean "can I test a string being equal to several strings in one operation", use regex:

if (something.matches("String1|String2|String3")) {
    System.out.println(Something);
} 

The pipe char | means "OR" in regex.

Note that in java (unlike many other languages) matches() must match the whole string - ie this is an "equals" comparison, not a "contains" comparison.

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533580

If something is "String1 String2 String3" then it is equal.

If you mean contains, you can do

List<String> valid = Arrays.asList(string1, string2, string3);

if (valid.contains(something))

Upvotes: 5

Thomas Jungblut
Thomas Jungblut

Reputation: 20969

No, but you can use || to test multiple strings for equality:

if(something.equals("String1") || something.equals("String2") || something.equals("String3"))){
   System.out.println(Something);
}

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122008

No you cannot. equals() takes only one object at a time.

As an alternative, you can try something like

if(something.equals("String1") || something.equals("String2") ||       
                                                 something.equals("String3")) {
   System.out.println(Something);
}

Upvotes: 4

Related Questions