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.

Syntax
int indexOf(int ch) returns index position for the given char value int indexOf(int ch, int fromIndex) returns index position for the given char value and from index int indexOf(String substring) returns index position for the given substring int indexOf(String substring, int fromIndex) returns index position for the given substring and from index
Example
class TestClass{ public static void main (String[] args){ String str = "Halo World!"; int idx1 = str.indexOf('d'); System.out.println("Index of 'd' is "+idx1); int idx3 = str.indexOf("World"); System.out.println("World is exist and position is "+idx3); int idx4 = str.indexOf("Halo",4); System.out.println("Position of Halo after index 4 is "+idx4); } }
Output
Index of 'd' is 9 World is exist and position is 5 Position of Halo after index 4 is -1

Key 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.

Syntax
int lastIndexOf(int ch)
Example
class TestClass { public static void main (String[] args){ String str = "Halo World!"; int idx1 = str.lastIndexOf('l'); System.out.println("last Index of 'l' is "+idx1); int idx2 = str.indexOf('l'); System.out.println("Index of 'l' is "+idx2); } }
Output
last Index of 'l' is 8 Index of 'l' is 2

Key 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.