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. Show
A string, say 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 Example – Get the index of first occurrence of a substringIn this example, we shall initialize two strings with variable names /** * 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 CaseIn this example, we ignore the case of both the strings and try to find the occurrence of string Please note that with the strings str1 and str2 in the below program, if you consider the case, indexOf() would return /** * 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 ConclusionIn 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. The Parameters
Substring to search for, coerced to a string. If the method is called with no arguments, position OptionalThe method returns the index of the first occurrence of the specified substring at a position greater than or equal to
Return valueThe
index of the first occurrence of Return value when using an empty search stringSearching for an empty search string produces strange results. With no second argument, or with a second argument whose value is less than the calling string's length, the return value is the same as the value of the second argument:
However, with a second argument whose value is greater than or equal to the string's length, the return value is the string's length:
In the former instance, the method behaves as if it found an empty string just after the position specified in the second argument. In the latter instance, the method behaves as if it found an empty string at the end of the calling string. DescriptionStrings are zero-indexed: The index of a string's first character is
The
Checking occurrencesWhen checking if a specific substring occurs within a string, the correct way to check is test whether the return value is
ExamplesUsing indexOf()
The following example uses
indexOf() and case-sensitivityThe following example defines two string variables. The variables contain the same string, except that the second string contains uppercase letters. The first
Using indexOf() to count occurrences of a letter in a string The following
example sets
Specifications
Browser compatibilityBCD tables only load in the browser See alsoWhat is used to find the first occurrence of a given string in another string?The function strstr returns the first occurrence of a string in another string. This means that strstr can be used to detect whether a string contains another string.
How do you find the first occurrence of a string?The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.
Which function finds first occurrence of character in string?C++ String find_first_of()
This function is used for finding the location of first occurrence of the specified characters.
Which function is used to find the last occurrence of a given string in another string?strrchr() — Locate Last Occurrence of Character in String
The strrchr() function finds the last occurrence of c (converted to a character) in string . The ending null character is considered part of the string .
|