Java switch-case 语句中不区分大小写的匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19180797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:49:16  来源:igfitidea点击:

Case insensitive matching in Java switch-case statement

javastringswitch-statementcase-sensitivecase-insensitive

提问by Punith Raj

I was wondering if there is a way to perform case insensitive match in java switch case statement. the default implementation is case sensitive. Please see the example below.

我想知道是否有办法在 java switch case 语句中执行不区分大小写的匹配。默认实现区分大小写。请看下面的例子。

public class SwitchCaseTest {

    /**
     * @param args
     */
     public static void main(String[] args) {

        switch ("UPPER") {
            case  "upper" :
                System.out.println("true");
                break;

            default:
                System.out.println("false");
                break;
        }
    }
}

So above statement returns falseas output. And i am trying make it work for case-insensitivematch like String.equalsIgnoreCase()would do. I tried to convert both the string literal to lower case and then compare. but was unable to do so.

所以上面的语句false作为输出返回。我正在尝试让它像会做的那样适用于不区分大小写的匹配String.equalsIgnoreCase()。我尝试将字符串文字都转换为小写,然后进行比较。但无法这样做。

采纳答案by ppeterka

If you want to do that: just make sure the input data is in all lowercase, and use lowercase cases...

如果你想这样做:只需确保输入数据全部小写,并使用小写...

switch ("UPPER".toLowerCase()) {
case  "upper" :

....

Localization issues

本地化问题

Also, the ages old issue of localization strikes again, and plagues this thing too... For example, in the Turkish Locale, the uppercase counterpart of iis not I, but ?... And in return, the Iis not transformed to i, but a "dotless i": ?. Don't underestimate this, it can be a deadly mistake...

此外,本地化的古老问题再次出现,并且也困扰着这件事......例如,在土耳其语区域设置中,大写对应的i不是I,但是?......作为回报,I不是转换为i,而是一个“无点我”:?。不要低估这一点,这可能是一个致命的错误......

回答by upog

try

尝试

switch ("UPPER".toUpperCase()) {
    case  "UPPER" :

回答by Omoro

You try making everything uppercase or lowercase

你试着把所有的东西都变成大写或小写

String str = "something".toUpperCase();
switch(str){
case "UPPER":
}

or

或者

String str = "something".toLowerCase();
swtich(str){
case "lower":
}

or even better use enum (note this is only possible from Java 7)

甚至更好地使用 enum (注意这只能从 Java 7 中实现)

enum YourCases {UPPER1, UPPER2} // cases.
YourCases c = YourCases.UPPER1; // you will probably get this value from somewhere
switch(c){
case YourCases.UPPER1: ....
break;
case YourCases.UPPER2: ....
}

回答by Chris Michael

To avoid having to use the case expression to verify if it is lowercase or uppercase, I recommend that you use the following:

为了避免必须使用 case 表达式来验证它是小写还是大写,我建议您使用以下内容:

String value = String.valueOf(userChoice).toUpperCase();

This helps to make the conversion of lowercase to uppercase before doing the evaluation in the switch case.

这有助于在 switch case 中进行评估之前将小写转换为大写。