Check if String Contains another String

To check if a string contains another string the method indexOf() is used. It returns the index in the haystack string where the needle string begins.
If the needle string is not present in the haystack string, the value -1 is returned.
In the code example below we check two needle strings against a haystack string, one exists and one does not.

If the code example above is executed, the output will be:

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?