Java Language Case insensitive switch


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!