我想匹配一个精确 string
在ArrayList<String>
.
目前这段代码将在myArrayList.contains(wordImLookingFor)
时执行.
if (myArrayList.contains(exactWordImLookingFor)) {
Toast.makeText(getApplicationContext(), "Match", Toast.LENGTH_SHORT).show();
}
总而言之,我正在寻找在输入整个字符串“dude”时执行的代码,而不仅仅是“d”或“du”或“dud”。
最佳答案
如果你想查看一个字符串是否在数组列表中试试这个:
for (String s : myArrayList)
{
if (s.equals(wordImLookingFor))
{
// Run your code here
}
}
或
if (myArrayList.contains(wordImLookingFor))
{
// Run your code here
}
如果你想查看输入的字符串是否是数组列表中任何内容的子字符串,试试这个:
for (String s : myArrayList)
{
if (s.contains(wordImLookingFor))
{
// Run your code here
}
}
这应该适用于包含“dude”和用户输入“d”的 myArrayList
示例。
关于java - 如何匹配 ArrayList<String> 中的精确字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24764328/