Java String indexOf() method example

HQ » Java Tutorial » java.lang » String » indexOf() method example
Java String indexOf() method example 2018-02-19T12:53:52+00:00

java.lang.String indexOf()

Description :

This java tutorial shows how to use the indexOf() method of java.lang.String class. This method returns an int datatype which which corresponds to the index of the string where the method argument str has been found. Note that since we are dealing with String, the index starts at 0.

Method Syntax :

public int indexOf(String str)

public int indexOf(String str, int fromIndex)

Parameter Input :

DataType Parameter Description
String str the string argument which we want to check the position on our String
int fromIndex index on our String where we want to start searching

Method Returns :

This method returns an int datatype which corresponds to the first occurrence index has been found. If the string parameter has not been found on our String, the value returned is -1.

Java Code Example :

This example source code demonstrates the use of indexOf() method of String class. Basically this source code just prints the index of the str method argument on the String object.

Sample Output :

Running the indexOf() method example source code of java.lang.String class will give you the following output

java string indexof method example

java string indexof method example

Suggested Reading List :


Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.