我正在尝试编写一个程序,要求用户输入一个字符串,说明三个给定选项之一。选项有:小、中、大。无论用户输入的单词的类型大小写(例如,小,中,LaRgE),有没有一种方法可以让我将字符串存储到我的变量中,因为第一个字母是大写,后面是所有小写 -大小写字母,或者甚至只是小、中、大的小写/大写的任意组合,最终会相应地变成小、中或大吗?
最佳答案
我认为您正在寻求一种方法来检查两个 String
的相等性,无论它们的大小写如何,而不是真正改变它们的值。您正在寻找equalsIgnoreCase
。根据甲骨文,
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.
String myString = "hello";
System.out.println(myString.equalsIgnoreCase("HELLO")); //true
System.out.println(myString.equalsIgnoreCase("hElLo")); //true
关于java - 在Java中,我可以扫描字符串并将其存储为小写/大写字符,无论输入如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32686706/