检查是否 ArrayList<String>包含字符串的一部分 [英] Check if ArrayList<String> contains part of a string
本文介绍了检查是否 ArrayList<String>包含字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个 ArrayList:
Say I have an ArrayList:
<string1.4>
<string2.4>
<string3.4>
当我说 arrayList.containsSubString('string1'); 时,我希望返回 arrayList 的第一个元素;除了遍历 arrayList
的每个元素并检查 string1
是否是该元素字符串的子字符串之外,如何做到这一点?
and I wish to return the the first element of the arrayList when I say arrayList.containsSubString('string1'); How could this be done other than iterating through each of the elements of the arrayList
and checking if string1
is a substring of that element string?
推荐答案
我能想到的唯一方法是做这样的事情:
The only way I can think of is doing something like:
strs.get(strs.indexOf(new Object() {
@Override
public boolean equals(Object obj) {
return obj.toString().contains(s);
}
}));
不知道这是否被认为是好的做法.
Don't know if it is considered good practice though.
这篇关于检查是否 ArrayList<String>包含字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文