String indexOf() Method in Java — Learn with Example-Shikshaglobe

Content Creator: Dinesh Kumar

The indexOf() method returns the position of the first occurrence of a specified value in a string.

This method returns -1 if the value to search for never occurs.

Note: The indexOf() method is case-sensitive.

Syntax

str.indexOf(str, fromIndex)

Example 1

public class IndexOfExample1 {

public static void main(String[] args) {

String str = "This is an example string";

System.out.println("Index of 's': " + str.indexOf('s'));

System.out.println("Index of 's' from 3rd position: " + str.indexOf('s', 3));

System.out.println("Index of first occurrence of 'is': " + str.indexOf("is"));

System.out.println("Index of last occurrence of 'is': " + str .

The indexOf() method is used to get the integer value of a particular index of a String type object, based on criteria specified by the programmer. This method returns -1 if the value to search for is not present in the string. If the value is present, it returns the position of the first occurrence of that value.

This method can be overloaded by specifying the starting index from which we need to search and also the ending index till which we need to search. We can also specify the character encoding scheme while using this method.

The indexOf() method is used to get the integer value of a particular index of a String type object, based on the criteria specified in the parameters. The first parameter of the indexOf() method is the character or substring that we want to find in the given string. If it is found, then it returns the position of that character or substring within the string. If it is not found, then it returns -1. The second parameter of this method is optional and specifies the starting position from where we want to search for the given character or substring.

The indexOf() method is used to get the integer value of a particular index of a String type object, based on the criteria specified in the parameters. The search for the character or substring begins from the specified start index and ends at the end of the string. If no such character or substring is found then -1 is returned.

For example,

String str = "Hello World!";

int index = str.indexOf('o'); // returns 4

If we consider the above example, the output will be 4 as 'o' is found at index 4 in "Hello World!" string.

The IndexOf() method in Java is used to find the index of a specified character or a substring in a given String. If the specified character or substring is not found, this method returns -1. This method can be overloaded with different signatures. The most common signature of this method is as follows:

int indexOf(String str)

Here, str is the string that has to be searched for within the invoking string object. The return type of this method is an int that represents the index value. If no match is found, -1 is returned.

This method can also be overloaded with a second parameter that denotes the starting index from where the search has to begin:

int indexOf(String str, int fromIndex)

In this case, if a match is found, its index value will be returned; otherwise -1 will be returned. If the fromIndex parameter's value is greater than or equal to the length of the

Tags:

string indexof() method in javastring indexof method in javastring indexof method in javascriptstring indexof method example in javajava indexof method examplestring indexof exampleexample of indexof in javastring indexof() method in java arrayliststring indexof() method in java arraystring indexof() method in java apistring indexof() method in java applicationstring indexof() method in java andstring indexof method in java appendstring indexof method in java addstring indexof method in java array containsstring indexof method in java anagramstring indexof method in java all occurrencesstring indexof() method in java bigdecimalstring indexof() method in java beanstring indexof() method in java belongs to which countrystring indexof() method in java belongsstring indexof() method in java belongs to which continentstring indexof methods in java beginners bookstring indexof methods in java builtstring indexof() method in java classstring indexof() method in java codestring indexof() method in java c#string indexof() method in java collectionsstring indexof methods in java corestring indexof methods in java conversionstring indexof() method in java docstring indexof() method in java definitionstring indexof() method in java definesstring indexof method in java deletestring indexof method in java differentstring indexof method in java datestring indexof method in java double quotestring indexof method in java dotstring indexof method in java denotesstring indexof method in java decodehow to use indexof method in javastring indexof() method in java for loopstring indexof() method in java functionstring indexof() method in java for stringstring indexof() method in java from another classstring indexof() method in java from same classstring indexof() method in java from another packagestring indexof() method in java geeksforgeeksstring indexof() method in java githubstring indexof() method in java graphicsstring indexof() method in java genericsstring indexof() method in java getstring indexof methods in java getterstring indexof() method in java httpstring indexof() method in java httpclientstring indexof() method in java howstring indexof() method in java hashmapstring indexof() method in java http requeststring indexof() method in java have variablesstring index method in java hashmapindexof method in string in javawhat is indexof()string indexof() method in java javastring indexof() method in java junitstring indexof() method in java javatpointstring indexof methods in java java 11string indexof() method in java kotlinstring indexof() method in java kustostring indexof() method in java karatestring indexof() method in java keywordstring indexof() method in java keystorestring indexof method in java kmp