How to use equalsignorecase() for multiple elements in Java

if(string.equalsIgnoreCase("first") || 
if(string.equalsIgnoreCase("first") || 
string.equalsIgnoreCase("second") || string.equalsIgnoreCase("third"))

I need to use 10 | here (I want to check 10 strings) There is no simple solution

I need to find the right conditions

Thank you in advance

Solution

If you need to know which match is true, use the switch (Java 7 only):

switch (string.toLowerCase())
{
    case "first": doSomething();
        break;
    case "second": ...;
        break;
    default: ...;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>