Java String indexOf() method

Facebooktwitterredditpinterestlinkedinmailby feather

Java String indexOf() method returns index of a given character or substring present in a String.

                 Methods                                                  Description

int indexOf(int ch)

 

It returns index position for the given char value.
int indexOf(int ch, int fromIndex)

 

It returns index position for the given char value and from index.
int indexOf(String substring)

 

It returns index position for the given substring.
int indexOf(String substring, int fromIndex)

 

It returns index position for the given substring and from index.

 Parameters:

ch: a single character

fromIndex: index position from where index of the char value or substring is returned

substring: substring to be searched in this string

Return:

It returns index of the character within a String

Java String IndexOf() method Example 1

Output:

Java String indexOf(String substring) Method Example 2

Output:

Java String indexOf(String substring, int fromIndex) Method Example 3

Output:

Facebooktwitterredditpinterestlinkedinmailby feather

Leave a Comment