How to match an EXACT String in ArrayList<String>

android, arraylist, java

Solution

If you want to see if a string is in the arraylist try this:

for (String s : myArrayList)
{
    if (s.equals(wordImLookingFor))
    {
        // Run your code here
    }
}

or

if (myArrayList.contains(wordImLookingFor))
{
    // Run your code here
}

If you want to see if a string entered is a substring of anything in the arraylist, try this:

for (String s : myArrayList)
{
    if (s.contains(wordImLookingFor))
    {
        // Run your code here
    }
}

This should work for your example of `myArrayList` containing "dude" and the user inputting "d".

Problem

I want to match an exact `string` in an `ArrayList<String>`. Currently this code will execute if `myArrayList.contains(wordImLookingFor)`. ``` if (myArrayList.contains(exactWordImLookingFor)) { Toast.makeText(getApplicationContext(), "Match", Toast.LENGTH_SHORT).show(); } ``` In summary, I'm looking for the code to execute when the entire String "dude" is entered, not just "d" or "du" or "dud".

Original source

Related problems