46

我正在做一个关于 Java 7 新特性的 POC。我有在 switch 语句中使用 String 的代码,它可以工作。我也想让它在不区分大小写的情况下工作。有没有办法在 String 上使用 ignoreCase 进行检查?

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

}
}
4

3 回答 3

103

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

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

在我们吹毛求疵的同时,您最好在英语语言环境中使用大写字母以避免土耳其语出现问题

于 2013-10-28T06:09:21.420 回答
9

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

 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);
            }
        }
    }
于 2013-10-28T06:20:41.953 回答
4

从 oracle docs切换字符串

将 switch 表达式中的 String 与与每个 case 标签关联的表达式进行比较,就好像使用了String#equals方法一样。

您可以使用

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

也可以看看

于 2013-10-28T06:15:21.977 回答