忽略Java中的大写和小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26997164/
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
Ignoring upper case and lower case in Java
提问by Noah Skull Weijian
I want to know how to make whatever the user inputs to ignore case in my method:
我想知道如何使用户输入的任何内容忽略我的方法中的大小写:
public static void findPatient() {
if (myPatientList.getNumPatients() == 0) {
System.out.println("No patient information is stored.");
}
else {
System.out.print("Enter part of the patient name: ");
String name = sc.next();
sc.nextLine();
System.out.print(myPatientList.showPatients(name));
}
}
采纳答案by Patrik H?ggren
You have to use the String method .toLowerCase()
or .toUpperCase()
on both the input and the string you are trying to match it with.
您必须使用 String 方法.toLowerCase()
或.toUpperCase()
在输入和您尝试与之匹配的字符串上使用。
Example:
例子:
public static void findPatient() {
System.out.print("Enter part of the patient name: ");
String name = sc.nextLine();
System.out.print(myPatientList.showPatients(name));
}
//the other class
ArrayList<String> patientList;
public void showPatients(String name) {
boolean match = false;
for(matchingname : patientList) {
if (matchingname.toLowerCase.contains(name.toLowerCase())) {
match = true;
}
}
}
回答by peterremec
use toUpperCase() or toLowerCase() method of String class.
使用 String 类的 toUpperCase() 或 toLowerCase() 方法。
回答by dieter
Use String#toLowerCase()
or String#equalsIgnoreCase()
methods
用途String#toLowerCase()
或String#equalsIgnoreCase()
方法
Some examples:
一些例子:
String abc = "Abc".toLowerCase();
boolean isAbc = "Abc".equalsIgnoreCase("ABC");
回答by alain.janinm
You ignore case when you treat the data, not when you retrieve/store it. If you want to store everything in lowercase use String#toLowerCase, in uppercase use String#toUpperCase.
处理数据时忽略大小写,而不是检索/存储数据时。如果要以小写形式存储所有内容,请使用String#toLowerCase,在大写形式中使用String#toUpperCase。
Then when you have to actually treat it, you may use out of the bow methods, like String#equalsIgnoreCase(java.lang.String). If nothing exists in the Java API that fulfill your needs, then you'll have to write your own logic.
然后,当您必须实际处理它时,您可以使用 out of the bow 方法,例如String#equalsIgnoreCase(java.lang.String)。如果 Java API 中不存在满足您需求的任何内容,那么您将不得不编写自己的逻辑。
回答by Naf Ty
I have also tried all the posted code until I found out this one
我也尝试了所有发布的代码,直到我发现了这个
if(math.toLowerCase(Locale.ENGLISH));
Here whatever character the user input will be converted to lower cases.
在这里,用户输入的任何字符都将转换为小写。
回答by user10018672
The .equalsIgnoreCase() method should help with that.
.equalsIgnoreCase() 方法应该对此有所帮助。