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

Content Creator: Dinesh Kumar

String indexOf() Method in Java

The indexOf() method in Java is a built-in method provided by the String class that is used to find the index (position) of the first occurrence of a specified character or substring within a given string. It returns the index of the first occurrence, or -1 if the character or substring is not found in the string.

Syntax:

int indexOf(int ch)

int indexOf(int ch, int fromIndex)

int indexOf(String str)

int indexOf(String str, int fromIndex)

Click here to learn more

Parameters:

  • ch: The character whose index is to be found.
  • fromIndex: The index from where the search begins.
  • str: The substring whose index is to be found.

Return Value:

  • The index of the first occurrence of the character or substring, or -1 if not found.

Example 1: Using indexOf(char ch) method

public class StringIndexOfExample {

    public static void main(String[] args) {

        String str = "Hello, World!";

        char ch = 'o';

 

        int index = str.indexOf(ch);

 

        if (index != -1) {

            System.out.println("The first occurrence of '" + ch + "' is at index: " + index);

        } else {

            System.out.println("'" + ch + "' not found in the string.");

        }

    }

}

 

Keep reading

Output

The first occurrence of 'o' is at index: 4

 

Example 2: Using indexOf(String str) method


public class StringIndexOfExample {

    public static void main(String[] args) {

        String str = "Java is awesome!";

        String subStr = "is";

 

        int index = str.indexOf(subStr);

 

        if (index != -1) {

            System.out.println("The first occurrence of \"" + subStr + "\" is at index: " + index);

        } else {

            System.out.println("\"" + subStr + "\" not found in the string.");

        }

    }

}

 

Find out more

Output:

The first occurrence of "is" is at index: 5

In the above examples, we use the indexOf() method to find the index of the first occurrence of a character and a substring in the given string. If the character or substring is found, the method returns the index; otherwise, it returns -1. The indexOf() method is useful for tasks like searching, extracting substrings, and manipulating strings in Java.

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

Take the next step

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


Click Here

Explore More

Best Discord Emoji MakerHow to Open EPS FileWhat is Business Analysis? Process & TechniquesWhat is Business Analyst? Roles, Responsibility, Skills, Certifications

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

Featured Universities

Mahatma Gandhi University

Location: Soreng ,Sikkim , India
Approved: UGC
Course Offered: UG and PG

MATS University

Location: Raipur, Chhattisgarh, India
Approved: UGC
Course Offered: UG and PG

Kalinga University

Location: Raipur, Chhattisgarh,India
Approved: UGC
Course Offered: UG and PG

Vinayaka Missions Sikkim University

Location: Gangtok, Sikkim, India
Approved: UGC
Course Offered: UG and PG

Sabarmati University

Location: Ahmedabad, Gujarat, India
Approved: UGC
Course Offered: UG and PG

Arni University

Location: Tanda, Himachal Pradesh, India.
Approved: UGC
Course Offered: UG and PG

Capital University

Location: Jhumri Telaiya Jharkhand,India
Approved: UGC
Course Offered: UG and PG

Glocal University

Location: Saharanpur, UP, India.
Approved: UGC
Course Offered: UG and PG

Himalayan Garhwal University

Location: PG, Uttarakhand, India
Approved: UGC
Course Offered: UG and PG

Sikkim Professional University

Location: Sikkim, India
Approved: UGC
Course Offered: UG and PG

North East Frontier Technical University

Location: Aalo, AP ,India
Approved: UGC
Course Offered: UG and PG