if(string.equalsIgnoreCase("first") ||
string.equalsIgnoreCase("second") || string.equalsIgnoreCase("third"))
I need to use 10 ||
here (I have 10 strings to check).Is there any simple solution for this.
And i need to find which condition is satisfied.
Thanks in advance..
You could use string.matches()
which takes a regular expression into which you can coerce first, second third etc:
if (string.matches("first|second|third"))
or, for case insensitivity:
if (string.matches("(?i)first|second|third"))
Regular expressions are complex though so could be a performance issue.
If you need to know which match is true, then use a switch (Java 7 only):
switch (string.toLowerCase())
{
case "first": doSomething();
break;
case "second": ...;
break;
default: ...;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With