As the name itself indicates startsWith
says if String
starts with certain text. To know contain the text anywhere and not specifically at the beginning use contains
:
String query = "Policia Militar";
query.contains("Policia"); // true
query.contains("Militar"); // true
However, please note that the search must be exact for lowercase, so if you search for militar
it will not be true:
query.contains("militar"); // false
If this is a problem, you can change the logic to convert both to upper case or lower case before the comparison, which is already resolved. This change can be made either with toLowerCase
such as toUpperCase
:
String pesquisa = "militar";
query.toLowerCase().contains(pesquisa.toLowerCase()); //true
See these examples on Ideone