How to search the whole string for a specific word?

You have three way to search if an string contain substring or not:

String string = "Test, I am Adam";
// Anywhere in string
b = string.indexOf("I am") > 0;         // true if contains 

// Anywhere in string
b = string.matches("(?i).*i am.*");     // true if contains but ignore case

// Anywhere in string
b = string.contains("AA")  ;             // true if contains but ignore case

Check out the contains(CharSequence) method


I have not enough 'reputation points' to reply in the comments, but there is an error in the accepted answer. indexOf() returns -1 when it cannot find the substring, so it should be:

    b = string.indexOf("I am") >= 0; 

Tags:

Java

Android