Java String indexOf()

The java string indexOf() method returns index of given character value or substring. If it is not found, it returns -1. The index counter starts from zero.


Internal implementation


Signature

There are 4 types of indexOf method in java. The signature of indexOf methods are given below:

No. Method Description
1 int indexOf(int ch) returns index position for the given char value
2 int indexOf(int ch, int fromIndex) returns index position for the given char value and from index
3 int indexOf(String substring) returns index position for the given substring
4 int indexOf(String substring, int fromIndex) returns index position for the given substring and from index

Parameters

ch: char value i.e. a single character e.g. 'a'

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

substring: substring to be searched in this string


Returns

index of the string


Java String indexOf() method example

Test it Now
2  8
5
3

Java String indexOf(String substring) Method Example

This method takes substring as an argument and returns index of first character of the substring.

Test it Now
index of substring 16

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

This method takes substring and index as arguments and returns index of first character occured after the given fromIndex.

Test it Now
index of substring 16
index of substring -1

Java String indexOf(int char, int fromIndex) Method Example

This method takes char and index as arguments and returns index of first character occured after the given fromIndex.

Test it Now
index of char 17
Next TopicJava String intern




Contact US

Email:[email protected]

String indexOf()
10/30