Your current implementation is correct. The suggested is not possible but the pseudo code would be implemented with multiple equal()
calls and ||
.
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)) {
//...
}
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"))
If you develop for Android KitKat or newer, you could also use a switch statement (see: Android coding with switch (String)). e.g.
switch(yourString)
{
case "john":
//do something for john
case "mary":
//do something for mary
}
if (Arrays.asList("John", "Mary", "Peter").contains(name)) {
}
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
}
Source: Stackoverflow.com