用户输入忽略大小写

发布于 2024-10-26 09:01:22 字数 533 浏览 1 评论 0原文

我正在阅读用户输入。我想知道如何将 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.

I am reading a user input. I was wondering how I would apply equalsIgnoreCase to the user input?

 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多情出卖 2024-11-02 09:01:22

如果您不需要List,您可以使用使用不区分大小写的比较器初始化的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");

现在,当您调用remove 时,参数的大小写不再重要。因此,以下两行都可以工作:

colors.remove("RED");

or

colors.remove("Red");

但这仅在您不需要 List 接口为您提供的排序时才有效。

If you don't need a List you could use a Set initialized with a case-insensitive comparator:

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:

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.

苍白女子 2024-11-02 09:01:22

equalsIgnoreCase 是 String 类的一个方法。

尝试

someString.equalsIgnoreCase(bufRead.readLine());

equalsIgnoreCase is a method of the String class.

Try

someString.equalsIgnoreCase(bufRead.readLine());
清泪尽 2024-11-02 09:01:22

如果你想忽略大小写,那么在检索时就不能这样做。

相反,当您将其放入列表中时,您需要将其移至全部大写或全部小写。

ArrayList<String> aListColors = new ArrayList<String>();
aListColors.add("Red".toUpperCase());
aListColors.add("Green".toUpperCase());
aListColors.add("Blue".toUpperCase());

那么,你可以稍后再做

aListColors.remove(rem.toUpperCase());

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());
那请放手 2024-11-02 09:01:22

由于 ArrayList.remove 方法使用 equals 而不是 equalsIgnoreCase,因此您必须自己迭代列表。

Iterator<String> iter = aListColors.iterator();
while(iter.hasNext()){
     if(iter.next().equalsIgnoreCase(rem))
     {
        iter.remove();
        break;
     }
}

Since the ArrayList.remove method uses equals instead of equalsIgnoreCase you have to iterate through the list yourself.

Iterator<String> iter = aListColors.iterator();
while(iter.hasNext()){
     if(iter.next().equalsIgnoreCase(rem))
     {
        iter.remove();
        break;
     }
}
感悟人生的甜 2024-11-02 09:01:22

集合中的删除方法是为了删除 equals() 中的元素而实现的,这意味着 "Red".equals("red") 为 false,并且您无法在列表中找到具有 equalsIgnoreCase 的方法。这仅对 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"));
    }
}

或不覆盖 equals 的解决方案:编写迭代列表并以 equalsIgnoreCase 方式找到“红色”的方法。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文