Course Content
Java Extended
Java Extended
indexOf() method
How to find an index of the specified character in String?
To find the index of the first occurrence of a specific letter, the String
class provides the indexOf()
method. This method is overloaded and has several implementations. Let's understand what each of them does:
indexOf(String str)
- In this method, the parameter specifies what we are searching for. It can be a single letter enclosed in double quotes (""
), a sequence of letters, or even a word. This method returns the index of the first occurrence of the specified parameter in the string. For example, let's find the index of the letter"l"
in the string"Hello"
:
Main
package com.example; public class Main { // do not change code from below public static void main(String[] args) { String hello = "Hello"; int index = hello.indexOf("l"); System.out.println("First appearance of the letter 'l' is on index " + index); } // do not change the code above }
In the above example, we initialized the variable index
using the method indexOf("l")
. Now, this variable contains the index of the first occurrence of the letter "l"
in the string "Hello"
.
indexOf(String str, int fromIndex)
- The first parameter is the same as before, specifying what we are searching for. The second parameter determines the starting index from where we begin the search. For example, in the word"Hello"
, there are 2 occurrences of the letter"l"
, and we want to find the index of the second occurrence. From the first example, we know that the first occurrence is at index2
, so let's set thefromIndex
parameter to 3 and find the index of the second"l"
:
Main
package com.example; public class Main { // do not change code from below public static void main(String[] args) { String hello = "Hello"; int firstIndex = hello.indexOf("l"); int secondIndex = hello.indexOf("l", firstIndex + 1); System.out.println("Second appearance of the letter 'l' is on index " + secondIndex); } // do not change the code above }
In the above example, we took a more practical approach. We used the location of the first occurrence of the specified letter to search for the second letter starting from the index increased by 1. This way, we begin the search from the next index after the index of the first occurrence of the desired letter or set of letters.
Additionally, the parameters String str
and int fromIndex
can be interchanged.
It is also worth noting that if the indexOf()
method does not find the specified letter or set of letters, it will return the value -1
. For example:
Main
package com.example; public class Main { // do not change code from below public static void main(String[] args) { String hello = "Hello"; int index = hello.indexOf("d"); System.out.println("Value of index, if it does not find a specified letter: " + index); } // do not change the code above }
The search for the letter "d"
in the string "Hello"
did not yield any results, and the method returned -1
. This can be useful for setting conditions or creating an exit point from a loop.
How to find the last index in the string
String
also has a method that allows searching from the end of the string. This method is called lastIndexOf()
. It operates on the same principle. Let's consider an example of finding the last occurrence of the letter "l"
in the string "Hello"
:
Main
package com.example; public class Main { // do not change code from below public static void main(String[] args) { String hello = "Hello"; int lastIndex = hello.lastIndexOf("l"); System.out.println("Last index of the letter 'l' : " + lastIndex); } // do not change the code above }
Here, we obtained result 3
, representing the index of the last occurrence of the letter "l"
.
Thanks for your feedback!