在 Java 7 中切换忽略大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19628157/
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
Switch ignore case in java 7
提问by CHowdappaM
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?
我正在做一个关于 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;
}
}
}
采纳答案by radai
no, but you could switch on s.toUpperCase()
. so:
不,但你可以打开s.toUpperCase()
。所以:
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
虽然我们在吹毛求疵,但您最好在英语语言环境中使用大写字母,以避免出现土耳其语问题
回答by Aniket Kulkarni
From oracle docs switch with string
从 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#equalsmethod were being used.
switch 表达式中的 String 与与每个 case 标签关联的表达式进行比较,就好像正在使用String#equals方法一样。
You can use
您可以使用
switch(s.toUpperCase()){
...
.....
}
See also
也可以看看
回答by Deepak Odedara
using String in switch Example from oracle docs Using Strings in switch Statements
在 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);
}
}
}