Search string snippets

Asked

Viewed 70 times

0

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

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

How do I search the entire content string?

  • Enter your code so we can help

  • Be clearer on your question, put the code or some example.

1 answer

3


As its name implies startsWith says whether the String begins 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 note that the search has to be accurate as far as uppercase and minuscule are concerned, so if you search for militar will not give true:

query.contains("militar"); // false

If this is a problem, you can change the logic to turn both to uppercase or minuscule before the comparison, which you already solve. This change can be made both with toLowerCase as with toUpperCase:

String pesquisa = "militar";
query.toLowerCase().contains(pesquisa.toLowerCase()); //true

See these examples in Ideone

  • Thank you very much, solved the problem !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.