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

Course Content

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

bookindexOf() 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)

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

copy
12345678910111213
package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { String hello = "Hello"; // find the index of the first appearance of the letter "l" int index = hello.indexOf("l"); // output the index of the first occurrence of "l" System.out.println("First appearance of the letter 'l' is on index " + index); } }

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)

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

copy
123456789101112131415
package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { String hello = "Hello"; // find the index of the first appearance of the letter "l" int firstIndex = hello.indexOf("l"); // find the index of the second appearance of the letter "l", starting from the position after the first "l" int secondIndex = hello.indexOf("l", firstIndex + 1); // output the index of the second occurrence of "l" System.out.println("Second appearance of the letter 'l' is on index " + secondIndex); } }

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

copy
12345678910111213
package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { String hello = "Hello"; // find the index of the letter "d" in the string int index = hello.indexOf("d"); // output the index, if the specified letter is not found, the value will be -1 System.out.println("Value of index, if it does not find a specified letter: " + index); } }

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

copy
12345678910111213
package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { String hello = "Hello"; // find the last index of the letter "l" int lastIndex = hello.lastIndexOf("l"); // output the last index where the letter "l" appears System.out.println("Last index of the letter 'l' : " + lastIndex); } }

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
some-alt