String.equals() with multiple conditions (and one action on result)

JavaAndroid

Java Problem Overview


Is it possible to do something like this in Java for Android (this is a pseudo code)

IF (some_string.equals("john" OR "mary" OR "peter" OR "etc."){
   THEN do something
}

?

At the moment this is done via multiple String.equals() condition with || among them.

Java Solutions


Solution 1 - Java

Possibilities:

  • Use String.equals():

      if (some_string.equals("john") ||
          some_string.equals("mary") ||
          some_string.equals("peter"))
      {
      }
    
  • Use a regular expression:

      if (some_string.matches("john|mary|peter"))
      {
      }
    
  • Store a list of strings to be matched against in a Collection and search the collection:

      Set<String> names = new HashSet<String>();
      names.add("john");
      names.add("mary");
      names.add("peter");
    
      if (names.contains(some_string))
      {
      }
    

Solution 2 - Java

if (Arrays.asList("John", "Mary", "Peter").contains(name)) {
}
  • This is not as fast as using a prepared Set, but it performs no worse than using OR.
  • This doesn't crash when name is NULL (same with Set).
  • I like it because it looks clean

Solution 3 - Java

Keep the acceptable values in a HashSet and check if your string exists using the contains method:

Set<String> accept = new HashSet<String>(Arrays.asList(new String[] {"john", "mary", "peter"}));
if (accept.contains(some_string)) {
    //...
}

Solution 4 - Java

Your current implementation is correct. The suggested is not possible but the pseudo code would be implemented with multiple equal() calls and ||.

Solution 5 - Java

If you develop for Android KitKat or newer, you could also use a switch statement (see: https://stackoverflow.com/questions/14367629/android-coding-with-switch-string/19722741#19722741). e.g.

switch(yourString)
{
     case "john":
          //do something for john
     case "mary":
          //do something for mary
}

Solution 6 - Java

No,its check like if string is "john" OR "mary" OR "peter" OR "etc."

you should check using ||

Like.,,if(str.equals("john") || str.equals("mary") || str.equals("peter"))

Solution 7 - Java

Pattern p = Pattern.compile("tom"); //the regular-expression pattern
Matcher m = p.matcher("(bob)(tom)(harry)"); //The data to find matches with

while (m.find()) {
	//do something???
}	

Use regex to find a match maybe?

Or create an array

 String[] a = new String[]{
        "tom",
        "bob",
        "harry"
 };
 
 if(a.contains(stringtomatch)){
     //do something
 }
	

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsandaloneView Question on Stackoverflow
Solution 1 - JavahmjdView Answer on Stackoverflow
Solution 2 - JavaSarsaparillaView Answer on Stackoverflow
Solution 3 - JavakrockView Answer on Stackoverflow
Solution 4 - JavaWarrenFaithView Answer on Stackoverflow
Solution 5 - JavaBoardyView Answer on Stackoverflow
Solution 6 - JavaSamir MangroliyaView Answer on Stackoverflow
Solution 7 - JavaFabianCookView Answer on Stackoverflow