Search for strings in string

0

In a java-filtered search using .startWith (query) for example:

I want to search for "Military Police" in the search I type "Police" and police appears, okay. If I type "military", it does not appear in my search.

How do I search the contents of the entire string ?

    
asked by anonymous 12.05.2018 / 13:43

1 answer

3

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

    
12.05.2018 / 13:58