Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
indexOf() method | String Advanced

indexOf() methodindexOf() 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":
java

Main.java

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 index 2, so let's set the fromIndex parameter to 3 and find the index of the second "l":
java

Main.java

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:

java

Main.java

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":

java

Main.java

Here, we obtained result 3, representing the index of the last occurrence of the letter "l".

Everything was clear?

Section 3. Chapter 4

indexOf() methodindexOf() 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":
java

Main.java

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 index 2, so let's set the fromIndex parameter to 3 and find the index of the second "l":
java

Main.java

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:

java

Main.java

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":

java

Main.java

Here, we obtained result 3, representing the index of the last occurrence of the letter "l".

Everything was clear?

Section 3. Chapter 4
some-alt