Switch ignore case in java 7

CHowdappaM picture CHowdappaM · Oct 28, 2013 · Viewed 56k times · Source

I am doing a POC on Java 7 new features. I have code to use String in switch statement and it works. I want to make it work in case insensitive also. Is there a way to check out with ignoreCase on String?

package com.java.j7;

public class Test {
    final private String _NEW ="NEW";
    final private String _PENDING = "PENDING";
    final private String _CLOSED = "CLOSED";
    final private String _REJECTED ="REJECTED";

public static void main(String... strings){

    Test j = new Test();
    j.processItem("new");
    j.processItem("pending");
    j.processItem("closed");
    j.processItem("rejected");

}

void processItem(String s){
    switch (s) {
    case _NEW:
        System.out.println("Matched to new");
        break;
    case _PENDING:
        System.out.println("Matched to pending");
        break;
    case _CLOSED:
        System.out.println("Matched to closed");
        break;
    case _REJECTED:
        System.out.println("Matched to rejected");
        break;

    default:
        System.out.println("Not matching any more");
        break;
    }

}
}

Answer

radai picture radai · Oct 28, 2013

no, but you could switch on s.toUpperCase(). so:

switch (s.toUpperCase()) {
   //same as before
}

and while we're nitpicking, you better upper-case things in the english locale to avoid issues with turkish