How to check if String contains another SubString in Java? contains() and indexOf() example

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accept a String and return starting position of the string if it exists, otherwise it will return -1. For example "fastfood".indexOf("food") will return 4 but "fastfood".indexOf("Pizza") will return -1. This is the easiest way to test if one String contains another substring or not. The second method is lastIndexOf() which is similar to indexOf() but start the search from the tail, but it will also return -1 if substring not found in the String or the last position of the substring, which could be anything between 0 and length -1.
Read more �

Posting Komentar

0 Komentar