Java String indexOf() method example

The Java String indexOf() method returns the index of given character or string as method argument. If argument is not found in string, method returns -1. The index counter for a string starts from zero.

Java String indexOf() method syntax

String indexOf() method has four overloaded forms:

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

‘null’ argument is not allowed

Passing null argument is not allowed to indexOf() method. It will result in NullPointerException exception.

String blogName = "howtodoinjava.com";
System.out.println( blogName.indexOf(null) );

//Program output

Exception in thread "main" java.lang.NullPointerException
	at java.lang.String.indexOf(String.java:1705)
	at java.lang.String.indexOf(String.java:1685)
	at com.StringExample.main(StringExample.java:9)

1. Java String indexOf(String substring) example

Java program to find index of substring in a given a string object using indexOf(String substring) method.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        
        System.out.println( blogName.indexOf("java") );			//9
        
        System.out.println( "hello world".indexOf("world") );	//6
        
        System.out.println( "hello world".indexOf("earth") );	//-1
    }
}

Program output.

9
6
-1

2. Java String indexOf(String substring, int fromIndex) example

Java program to find index of substring in a given a string object, starting for given fromIndex using indexOf(String substring, int fromIndex) method.

Please note that when a substring is found, index counting starts with 0 index and from beginning of string only.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        
        System.out.println( blogName.indexOf("java", 5) );			//9
        System.out.println( "hello world".indexOf("world", 6) );	//6
        System.out.println( "hello world".indexOf("world", 2) );	//6
        System.out.println( "hello world".indexOf("world", 10) );	//-1
    }
}

Program output.

9
6
6
-1

3. Java String indexOf(char ch) example

Java program to find index of given character 'ch' in a given a string object using indexOf(char ch) method.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        
        System.out.println( blogName.indexOf('j') );            //9
        System.out.println( "hello world".indexOf('w') );       //6
        System.out.println( "hello world".indexOf('k') );       //-1
    }
}

Program output.

9
6
-1

4. Java String indexOf(int ch, int fromIndex) example

Java program to find index of character 'ch' in a given a string object, starting for given fromIndex location using indexOf(String substring, int fromIndex) method.

Please note that when the character is found, index counting starts with 0 index and from beginning of string only.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        
        System.out.println( blogName.indexOf('j', 4) );         //9
        System.out.println( "hello world".indexOf('w', 2) );    //6
        System.out.println( "hello world".indexOf('w', 6) );    //6
        System.out.println( "hello world".indexOf('k') );       //-1
    }
}

Program output.

9
6
6
-1

Happy Learning !!

Java String Doc

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial