Java CompareToIgnoreCase

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12337001/
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-10-31 08:28:41  来源:igfitidea点击:

Java CompareToIgnoreCase

javastringstring-comparison

提问by user1645034

im creating a method where a user input has to type A/B to get a return. how do i make my code case insensitive to make it work?

我正在创建一种方法,其中用户输入必须键入 A/B 才能获得回报。我如何使我的代码不区分大小写以使其工作?

 String examcode (String code) {
    if (code.compareToIgnoreCase("A")) {
        EXAM_NAME = "Subject A";
    }
    else if (code.compareToIgnoreCase("B")) {
         EXAM_NAME = "Subject B";
    }
    else {
        EXAM_NAME = "no code";
    }
    return EXAM_NAME;
}

回答by Mark Peters

Use equalsIgnoreCase(), not compareToIgnoreCase().

使用equalsIgnoreCase(),不是compareToIgnoreCase()

if (code.equalsIgnoreCase("A")) {
   EXAM_NAME = "Subject A";
} //...

If you want to use compareToIgnoreCase()to check equality, you need to compare the result to 0:

如果要用于compareToIgnoreCase()检查相等性,则需要将结果与0

if (code.compareToIgnoreCase("A") == 0) {
    //...

just like any other compareTomethod. But there's no advantage to using it here.

就像任何其他compareTo方法一样。但是在这里使用它没有任何好处。

Edit:

编辑:

An alternative to your entire approach might be to use a case-insensitive map instead. Something like this:

整个方法的替代方法可能是使用不区分大小写的映射。像这样的东西:

Map<String, String> subjects = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

subjects.put("A", "Subject A");
subjects.put("B", "Subject B");

//...
String subjectName = subjects.get(code);
if ( subjectName == null ) {
    subjectName = "no code";
}

Or, you could also use a normal case-sensitive map and just convert all keys to lower case before doing map operations.

或者,您也可以使用普通的区分大小写的映射,并在执行映射操作之前将所有键转换为小写。

回答by Bhesh Gurung

Just use the method - String.equalsIgnoreCase(java.lang.String)which returns a booleanand does the job for what you are looking for.

只需使用该方法 - String.equalsIgnoreCase(java.lang.String)它返回 aboolean并完成您正在寻找的工作。

if (code.compareToIgnoreCase("A"))is not valid since compareToIgnoreCasereturns int.

if (code.compareToIgnoreCase("A"))无效,因为compareToIgnoreCase返回int

回答by Richard Sitze

Reading the javadoc for Stringwould have provided your answer for you. Google would have turned that right up.

阅读Stringjavadoc 将为您提供答案。谷歌本来可以做到这一点的。

  • Use code.equalsIgnoreCase("your string")which returns a boolean value, OR

  • use code.compareToIgnoreCase("your string") == 0. compareToIgnoreCasegives you a way to determine the "order" of two strings. Again, enjoy the javadoc.

  • 使用code.equalsIgnoreCase("your string")它返回一个布尔值,或

  • 使用code.compareToIgnoreCase("your string") == 0. compareToIgnoreCase为您提供了一种确定两个字符串的“顺序”的方法。再次享受 javadoc。

回答by jelies

Use same code but equalsIgnoreCase(String) instead of compareToIgnoreCase(String).

使用相同的代码,但使用 equalsIgnoreCase(String) 而不是 compareToIgnoreCase(String)。

回答by qqilihq

What you want is String#equalsIgnoreCase(String)which returns a boolean.

你想要的是String#equalsIgnoreCase(String)返回一个布尔值。

回答by Frank Thomas

use String.toLowerCase() (or String.toUpperCase()) before comparing.

在比较之前使用 String.toLowerCase()(或 String.toUpperCase())。

if (code.toLowerCase().equals("a")) {
    EXAM_NAME = "Subject A";
}

Note that like all string methods, code.toLowerCase does not change the string stored in 'code', but returns a copy of 'code's value all in lower case.

请注意,与所有字符串方法一样,code.toLowerCase 不会更改存储在“code”中的字符串,而是以小写形式返回“code”值的副本。