Switch ignore case in java 7

前端 未结 3 1398
渐次进展
渐次进展 2020-12-29 18:54

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

相关标签:
3条回答
  • 2020-12-29 19:16

    From oracle docs switch with string

    The String in the switch expression is compared with the expressions associated with each case label as if the String#equals method were being used.

    You can use

    switch(s.toUpperCase()){
    ...
    .....
    }
    

    See also

    • String#toUpperCase
    0 讨论(0)
  • 2020-12-29 19:17

    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

    0 讨论(0)
  • 2020-12-29 19:21

    using String in switch Example from oracle docs Using Strings in switch Statements

     public class StringSwitchDemo {
    
            public static int getMonthNumber(String month) {
    
                int monthNumber = 0;
    
                if (month == null) {
                    return monthNumber;
                }
    
                switch (month.toLowerCase()) {
                    case "january":
                        monthNumber = 1;
                        break;
                    case "february":
                        monthNumber = 2;
                        break;
                    case "march":
                        monthNumber = 3;
                        break;
                    case "april":
                        monthNumber = 4;
                        break;
                    case "may":
                        monthNumber = 5;
                        break;
                    case "june":
                        monthNumber = 6;
                        break;
                    case "july":
                        monthNumber = 7;
                        break;
                    case "august":
                        monthNumber = 8;
                        break;
                    case "september":
                        monthNumber = 9;
                        break;
                    case "october":
                        monthNumber = 10;
                        break;
                    case "november":
                        monthNumber = 11;
                        break;
                    case "december":
                        monthNumber = 12;
                        break;
                    default: 
                        monthNumber = 0;
                        break;
                }
    
                return monthNumber;
            }
    
            public static void main(String[] args) {
    
                String month = "August";
    
                int returnedMonthNumber =
                    StringSwitchDemo.getMonthNumber(month);
    
                if (returnedMonthNumber == 0) {
                    System.out.println("Invalid month");
                } else {
                    System.out.println(returnedMonthNumber);
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题