Java Language Strings Case insensitive switch

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Java SE 7

switch itself can not be parameterised to be case insensitive, but if absolutely required, can behave insensitive to the input string by using toLowerCase() or toUpperCase:

switch (myString.toLowerCase()) {
     case "case1" :
        ...            
     break;
     case "case2" :
        ...            
     break;
}

Beware

  • Locale might affect how changing cases happen!
  • Care must be taken not to have any uppercase characters in the labels - those will never get executed!


Got any Java Language Question?