在 java 7 中切换忽略大小写

2022-08-31 23:50:13

我正在Java 7新功能上做一个POC。我有代码在开关语句中使用字符串,它可以工作。我想让它在不敏感的情况下工作。有没有办法用 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;
    }

}
}

答案 1

不,但您可以打开 .所以:s.toUpperCase()

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

当我们挑剔时,您最好在英语区域设置中大写,以避免土耳其语问题


答案 2

在交换机中使用字符串 来自 oracle 文档的示例 在交换机语句中使用字符串

 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);
            }
        }
    }