0% found this document useful (0 votes)
56 views

Get The Index of First Occurrence of A Substring in A String Using Java

To find the index of the first occurrence of a substring in a string in Java, use the String.indexOf() method. It returns the index of the first occurrence of the substring. If the substring is not found, it returns -1. For example, if searching the string "hello world good morning. good day." for the substring "good", it would return 12, the index of the first occurrence. This method can also ignore case by converting both strings to lowercase first.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Get The Index of First Occurrence of A Substring in A String Using Java

To find the index of the first occurrence of a substring in a string in Java, use the String.indexOf() method. It returns the index of the first occurrence of the substring. If the substring is not found, it returns -1. For example, if searching the string "hello world good morning. good day." for the substring "good", it would return 12, the index of the first occurrence. This method can also ignore case by converting both strings to lowercase first.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

How to get the index of first occurrence of a

substring in a String using Java ?

Get the index of the first occurrence of a substring in a String using


Java?

To find the index of first occurrence of a substring in a string you can use String.indexOf() function.

A string, say str2 , can occur in another string, say str1 , n number of times. There could be a
requirement in your Java application, that you have to find the position of the first occurrence of str2 in
str1 . Or you may need to find nth occurrence.

In this tutorial, we shall learn to get the index of first occurrence of a string in another string using Java.

We shall make use of String.indexOf(String otherString) method, to find the index of first
occurrence of string str2 in str1 . If the string str2 is present in the other string str1 , it returns the
index of its first occurrence. If it is not present, then it returns -1 indicating that the string str2 is not
present in the string str1 .

Example – Get the index of first occurrence of a substring

In this example, we shall initialize two strings with variable names str1 and str2 . And we are going to find
the first occurrence of str2 in str1 .

/**
* Java Example program to find the index of first occurrence of a substring in a string
*/
public class FirstOccurrenceExample {

public static void main(String[] args) {


//initialize strings
String str1 = "hello world good morning. good day.";
String str2 = "good";

//get the index of str2 in str1


int indexOfSubStr = str1.indexOf(str2);

System.out.println("str2 first appeared in str1 at index : "+ indexOfSubStr);


}
}
When the above program is run, the output to the console is as shown below :

str2 first appeared in str1 at index : 12

Example – Get the index of first occurrence of a substring – Ignore Case

In this example, we ignore the case of both the strings and try to find the occurrence of string str2 in string
str1 . To ignore the case, we have actually converted the strings to lowercase and then applied the function
indexOf().

Please note that with the strings str1 and str2 in the below program, if you consider the case, indexOf() would
return -1 . But as we are ignoring the case, we got str2="GOOD" and GoOd in str1 matched.

/**
* Java Example program to find the index of first occurrence of a substring in a string
*/
public class FirstOccurrenceExample {

public static void main(String[] args) {


//initialize strings
String str1 = "hello world GoOd morning. gOOD day.";
String str2 = "GOOD";

//ignore case and get the index of str2 in str1


int indexOfSubStr = str1.toLowerCase().indexOf(str2.toLowerCase());

System.out.println("str2 first appeared in str1 at index : "+ indexOfSubStr);


}
}

Run the program.

str2 first appeared in str1 at index : 12

Conclusion

In this Java Tutorial, we have learned how to get index of first occurrence of a substring in a String, by
considering or ignoring the case of alphabets.

✦ Java Tutorial

Java String Operations


Java String Operations

✦ Java String

✦ Java String Operations

✦ Java - Print String to Console

✦ Java - Read String from Console

✦ Java - Concatente two Strings

✦ Java - Check if Strings are Equal

➩ Java - Find Index of First Occurrence of Substring

✦ Java - Find Index of Nth Occurrence of Substring

✦ Java - Replace First Occurrence of Substring

✦ Java - Replace All Occurrences of Substring

✦ Java - Reverse a String

✦ Java - Split String

✦ Java - Trim String

You might also like