我有一个包含(比方说)三个条目的数组列表。
The first entry: "Chris lives at 2320 alpha street" The second entry: "Bob sleeps at 1900 bravo road" The third entry: "Alex eats at 4231 charlie lane"
I'm trying to search through the arraylist, parse each line for a term the user enters, and return (to another part of my code) the index of the arraylist entry that contains the search term. In this example, the user types in "lane", the code searches all three, finds that the third entry contains the word "lane", and then returns a 2.
Currently, I'm using
if(addressBook.contains(searchModifier))//if their searchModifier is found anywhere in any of the strings
return addressBook.indexOf(searchModifier);//return that index value
else
return noEntry;
但这仅在存在完全匹配的字符串时才有效。我需要能够仅搜索字符串的一部分。有没有一种简单的方法可以做到这一点?我检查了this question ,但对于我想要做的事情来说,这似乎非常复杂 - 这是一个“hello world”类型的程序。我最多只搜索 3-4 个条目! (更不用说我对此还很陌生,而且代码看起来非常先进......)
最佳答案
for (int i = 0; i < addressBook.size(); i++) {
if (addressBook.get(i).contains(searchModified)) {
return i;
}
}
关于java - 在数组列表的条目中搜索一段文本的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22751383/