gpt4 book ai didi

java - 通过忽略区分大小写从列表中删除单词

转载 作者:行者123 更新时间:2023-12-03 05:24:18 25 4
gpt4 key购买 nike

我想从给定字符串中删除 ArrayList 中出现的所有单词。

我的相框上有 3 个按钮。一个添加单词,第二个删除单词,第三个显示单词。

我有一个名为 textvalue 的文本框和名为 mylist 的数组列表

我用过:

 textValue = text.getText().toLowerCase().trim();
if (mylist.contains(textValue)) {
mylist.removeAll(Arrays.asList(textValue));
label.setText("All occurrences of " + textValue + "removed");
} else {
label.setText("Word not found.");
}

如果我输入例如:mark和MARK,它仍然只会删除mark。

我也尝试过:

textValue = text.getText().toLowerCase().trim();
for (String current : mylist) {
if (current.equalsIgnoreCase(textValue)) {
mylist.removeAll(Collections.singleton(textValue));
label.setText("All occurrences of " + textValue + " removed");
} else {
label.setText("Word not found.");
}

}

最佳答案

只需使用 removeIf

mylist.removeIf(value->value.equalsIgnoreCase(textValue));

removeIf 接受 Predicate作为参数,所以您正在定义相应的 lambda expression通过忽略区分大小写来删除与 textValue 匹配的所有值

关于java - 通过忽略区分大小写从列表中删除单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58629161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com