Switch ignore case in java 7(在 Java 7 中切换忽略大小写)
问题描述
我正在对 Java 7 的新功能进行 POC.我有在 switch 语句中使用 String 的代码,它可以工作.我也想让它在不区分大小写的情况下工作.有没有办法在 String 上使用 ignoreCase 进行检查?
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?
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;
}
}
}
推荐答案
不,但你可以打开 s.toUpperCase().所以:
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
这篇关于在 Java 7 中切换忽略大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 7 中切换忽略大小写
基础教程推荐
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
