String indexOf() method in Java
The Java String indexOf() method is used to find the position of a specified string or character within a given string. It returns the index of the first occurrence of the target string or character within the string, or -1 if the target is not found.
SyntaxKey points about the String indexOf() method
Case-Sensitive Comparison:
By default, the indexOf() method performs a case-sensitive search. This means that uppercase and lowercase characters are considered different.
For example, in the string "Hello World", the indexOf("o") method would return the index of the first lowercase 'o', which is 4. If you search for an uppercase 'O', it would return -1.
Specifying the Target:
You can pass either a String or a char as the target to search for.
If a String is passed, the indexOf() method searches for the first occurrence of the specified substring within the string.
If a char is passed, the indexOf() method searches for the first occurrence of the specified character within the string.
Starting Index:
The indexOf() method also allows you to specify a starting index from where the search should begin. This can be useful if you want to find subsequent occurrences of the target after the first occurrence.
For example, if you have the string "Hello World" and you call indexOf("o", 5), it will start searching from index 5 and return the index of the second occurrence of 'o', which is 7.
Java String lastIndexOf()
The Java String lastIndexOf() method is used to find the index of the last occurrence of a specified string or character within a given string. It performs a right-to-left search inside the string and returns the index of the last occurrence of the target, or -1 if the target is not found.
SyntaxKey points about the String lastIndexOf() method
Right-to-Left Search:
The lastIndexOf() method performs a search from the end of the string towards the beginning.
It starts searching for the target string or character from the last index of the string and continues towards the beginning until it finds a match or reaches the start of the string.
Specifying the Target:
You can pass either a String or a char as the target to search for.
If a String is passed, the lastIndexOf() method searches for the last occurrence of the specified substring within the string.
If a char is passed, the lastIndexOf() method searches for the last occurrence of the specified character within the string.
Starting Index:
The lastIndexOf() method also allows you to specify a starting index from where the search should begin. This can be useful if you want to find the last occurrence of the target before a certain index.
The search starts from the specified starting index and continues towards the beginning of the string.
If the starting index is greater than or equal to the length of the string, the search will not be performed and -1 will be returned.
Conclusion
The indexOf() method in Java is used to find the index of the first occurrence of a specified substring within a given string. It returns the index value of the substring if found, or -1 if the substring is not present in the string. This method provides a convenient way to locate and work with substrings in a larger string.