Java String indexOf method

The primary functionality is to return the first index of a character or substring within a subject string.

1. public int indexOf(int ch)

Returns the index of the first occurrence of this character ch in the subject string .The index is in the range 0 to the string’s length-1 i.e (0<index<length-1).

If the character ch does not exist in the string ,-1 is returned.

        Example 1

        String str = "I love programming in Java.";

        System.out.println("First index of a:" + str.indexOf('a'));//12

        System.out.println("First index of L:" + str.indexOf('L'));// -1

2. public int indexOf(int ch,int fromIndex)

 Returns the index of the first occurrence of the character ch starting from fromIndex up to length-1.

If fromIndex is negative,then the entire string is searched starting from the beginning at index 0.If fromIndex is greater than the length of the string,-1 is returned.If fromIndex is equal to the length of the string ,-1 is returned as well.

If no match exists entirely in the range 0<fromIndex<length-1 , -1 is returned as well.

         Example 2

       String str = "I love programming in Java.";

       System.out.println("First index of a fromIndex 15:" + str.indexOf('a', 15));// 23

        System.out.println("First index of a fromIndex -6:" + str.indexOf('a', -6));// 12

        System.out.println("First index of a fromIndex 30:" + str.indexOf('a', 30));// -1

        System.out.println("First index of a fromIndex 26:" + str.indexOf('.', 26));// 26

        System.out.println("First index of a fromIndex 27(length of string):" + str.indexOf('.', 27));//-1

 

3. public int indexOf(String str)

Returns the index of the first occurrence of the substring str within this string.If str does not exist,-1 is returned.

Example 3

  String str = "I love programming in Java.";

  System.out.println("First index of programming :" + str.indexOf("programming"));// 7

  System.out.println("First index of code :" + str.indexOf("code"));// -1

 

4. public int indexOf(String str,int fromIndex)

Returns the index of the first occurrence of the substring str within this string object.If no occurrence is found,-1 is returned.

If fromIndex is negative,the substring str searching begins at index 0 and whole string is searched.

If fromIndex is equal to the length of the string or greater than the length of the string -1 is returned.

 Example 4.

 

      String str = "I love programming in Java.";

      System.out.println("First index of programming fromIndex 4:" + str.indexOf("programming", 4));//7

      System.out.println("First index of programming fromIndex -2:" + str.indexOf("programming", -2));// 7

       System.out.println("First index of a fromIndex 20:" + str.indexOf("a", 20));// 23

        System.out.println("First index of a fromIndex -2:" + str.indexOf("a", -2));// 12

        System.out.println("First index of programming fromIndex 10:" + str.indexOf("programming", 10));// -1

        System.out.println("First index of in fromIndex 10:" + str.indexOf("in", 10));// 15

        System.out.println("First index of in fromIndex 27:" + str.indexOf("in", 27));// -1

        System.out.println("First index of programming fromIndex 34:" + str.indexOf("programming", 34));// -1

 

 

Complete program for the above examples.

 

package devsought;

 

public class StringIndexOf {

    public static void main(String... args) {

        //public int indexOf(int ch)

        String str = "I love programming in Java.";

        System.out.println(str.length());

        System.out.println("First index of a:" + str.indexOf('a'));//prints 12

        System.out.println("First index of L:" + str.indexOf('L'));//prints -1

 

        //public int indexOf(int ch,int fromIndex)

        System.out.println("First index of a fromIndex 15:" + str.indexOf('a', 15));//prints 23

 

        System.out.println("First index of a fromIndex -6:" + str.indexOf('a', -6));//prints 12

 

        System.out.println("First index of a fromIndex 30:" + str.indexOf('a', 30));//prints -1

        System.out.println("First index of a fromIndex 26:" + str.indexOf('.', 26));//prints 26

        System.out.println("First index of a fromIndex 27(length of string):" + str.indexOf('.', 27));//prints -1

 

        //public int indexOf(String str)

        System.out.println("First index of programming :" + str.indexOf("programming"));//prints 7

 

        System.out.println("First index of code :" + str.indexOf("code"));//prints -1

 

        //public int indexOf(String str,int fromIndex)

        System.out.println("First index of programming fromIndex 4:" + str.indexOf("programming", 4));//prints 7

 

        System.out.println("First index of programming fromIndex -2:" + str.indexOf("programming", -2));//prints 7

 

        System.out.println("First index of a fromIndex 20:" + str.indexOf("a", 20));//prints 23

        System.out.println("First index of a fromIndex -2:" + str.indexOf("a", -2));//prints 12

 

        System.out.println("First index of programming fromIndex 10:" + str.indexOf("programming", 10));//prints -1

        System.out.println("First index of in fromIndex 10:" + str.indexOf("in", 10));//prints 15

        System.out.println("First index of in fromIndex 27:" + str.indexOf("in", 27));//prints -1

 

        System.out.println("First index of programming fromIndex 34:" + str.indexOf("programming", 34));//prints -1

 

    }

}

 

Output

27

First index of a:12

First index of L:-1

First index of a fromIndex 15:23

First index of a fromIndex -6:12

First index of a fromIndex 30:-1

First index of a fromIndex 26:26

First index of a fromIndex 27(length of string):-1

First index of programming :7

First index of code :-1

First index of programming fromIndex 4:7

First index of programming fromIndex -2:7

First index of a fromIndex 20:23

First index of a fromIndex -2:12

First index of programming fromIndex 10:-1

First index of in fromIndex 10:15

First index of in fromIndex 27:-1

First index of programming fromIndex 34:-1

About the Author - John Kyalo Mbindyo(Bsc Computer Science) is a Senior Application Developer currently working at NCBA Bank Group,Nairobi- Kenya.He is passionate about making programming tutorials and sharing his knowledge with other software engineers across the globe. You can learn more about him and follow him on  Github.