java 用户输入忽略大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5448379/
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
user input ignore case
提问by Some Java Guy
I am reading a user input. I was wondering how I would apply equalsIgnoreCase
to the user input?
我正在阅读用户输入。我想知道我将如何应用于equalsIgnoreCase
用户输入?
ArrayList<String> aListColors = new ArrayList<String>();
aListColors.add("Red");
aListColors.add("Green");
aListColors.add("Blue");
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
String rem = bufRead.readLine(); // the user can enter 'red' instead of 'Red'
aListColors.remove(rem); //equalsIgnoreCase or other procedure to match and remove.
回答by a_horse_with_no_name
If you don't need a List
you could use a Set
initialized with a case-insensitive comparator:
如果你不需要 aList
你可以使用一个Set
不区分大小写的比较器初始化:
Set<String> colors =
new TreeSet<String>(new Comparator<String>()
{
public int compare(String value1, String value2)
{
// this throw an exception if value1 is null!
return value1.compareToIgnoreCase(value2);
}
});
colors.add("Red");
colors.add("Green");
colors.add("Blue");
Now when you call remove, the case of the argument no longer matters. So both of the following lines would work:
现在,当您调用 remove 时,参数的大小写不再重要。所以以下两行都可以工作:
colors.remove("RED");
or
或者
colors.remove("Red");
But this will only work if you don't need the ordering that the List
interfaces gives you.
但这只有在您不需要List
接口给您的顺序时才有效。
回答by lukastymo
Method remove in collections is implemeneted to remove elements in equals() meaning so "Red".equals("red") is false and you can't find method which has equalsIgnnoreCase in List. This would have sense only for String so you can write your own class and add equals method - what is equals to you
集合中的方法 remove 被实现以删除 equals() 中的元素,这意味着“Red”.equals(“red”) 是假的,并且您无法在 List 中找到具有 equalsIgnnoreCase 的方法。这仅对 String 有意义,因此您可以编写自己的类并添加 equals 方法 - 什么等于您
class Person {
String name;
// getter, constructor
@Override
public boolean equals(Object obj) {
return (obj instanceof Person && ((Person)obj).getName().equalsIgnoreCase(name));
}
}
public class MyHelloWorld {
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
list.add(new Person("Red"));
list.remove(new Person("red"));
}
}
Or solution without override equals: write method which iterate through list and find your "red" in equalsIgnoreCase way.
或者没有覆盖 equals 的解决方案: write 方法遍历列表并以 equalsIgnoreCase 方式找到您的“红色”。
回答by MAK
equalsIgnoreCase is a method of the String class.
equalsIgnoreCase 是 String 类的一个方法。
Try
尝试
someString.equalsIgnoreCase(bufRead.readLine());
回答by corsiKa
IF you want to ignore the case, you can't do it when you retrieve.
如果要忽略大小写,检索时无法进行。
Instead, you need to move it to all caps or all lowercase when you put it in the list.
相反,当您将其放入列表时,您需要将其移动到全部大写或全部小写。
ArrayList<String> aListColors = new ArrayList<String>();
aListColors.add("Red".toUpperCase());
aListColors.add("Green".toUpperCase());
aListColors.add("Blue".toUpperCase());
Then, you can do later
然后,你可以稍后做
aListColors.remove(rem.toUpperCase());
回答by josefx
Since the ArrayList.remove method uses equals instead of equalsIgnoreCase you have to iterate through the list yourself.
由于 ArrayList.remove 方法使用 equals 而不是 equalsIgnoreCase,因此您必须自己遍历列表。
Iterator<String> iter = aListColors.iterator();
while(iter.hasNext()){
if(iter.next().equalsIgnoreCase(rem))
{
iter.remove();
break;
}
}