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 syntaxDescription
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

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java String indexOf() method example”

  1. Hey I see in many places
    int indexOf(int ch)
    Why int ch (not char ch)?
    I suppose it is a type but I see it in many places on here or I do not understand how the method works?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.