String indexOf() method example in Java

The indexOf() method of String class determines the index of the first occurrence of a particular String or a character within a String object. If the argument is not found in string, the method returns -1.

There are 4 versions of String indexOf method

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

Passing null argument

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

public class StringIndexOfExample0 { public static void main(String[] arr) { String str = new String("Welcome to Java programming.The Java is good programming language."); System.out.println(str.indexOf(null)); } } output: Exception in thread "main" java.lang.NullPointerException at java.lang.String.indexOf(String.java:1718) at java.lang.String.indexOf(String.java:1698) at StringIndexOfExample0.main(StringIndexOfExample0.java:5)

1). indexOf(char ch)

It returns the first index of the char ch found in the String object.

public class StringIndexOfExample1 { public static void main(String[] arr) { String str1 = new String("Welcome to java programming!!!"); int index1 = str1.indexOf('a'); int index2 = str1.indexOf('p'); int index3 = str1.indexOf('o'); System.out.println("First Index of 'a' in " + str1 + " : " + index1); System.out.println("First Index of 'p' in " + str1 + " : " + index2); System.out.println("First Index of 'o' in " + str1 + " : " + index3); } } output: First Index of 'a' in Welcome to java programming!!! : 12 First Index of 'p' in Welcome to java programming!!! : 16 First Index of 'o' in Welcome to java programming!!! : 4

2). indexOf(char ch, int fromIndex )

This method returns the index position for the given char value and from fromIndex location.

public class StringIndexOfExample2 { public static void main(String[] arr) { String str = new String("Welcome to Java programming.The Java is good programming language."); System.out.println("the String is : "+str); int index1 = str.indexOf("p",1); int index2 = str.indexOf("J",4); int index3 = str.indexOf("W",0); int index4 = str.indexOf("g",12); int index5 = str.indexOf("x",0); System.out.println("\nFirst Index of programming is : "+ index1); System.out.println("First Index of java is : "+ index2); System.out.println("First Index of Welcome is : "+ index3); System.out.println("First Index of good is : "+ index4); System.out.println("First Index of Hello is : "+ index5); } } output: the String is : Welcome to Java programming.The Java is good programming language. First Index of programming is : 16 First Index of java is : 11 First Index of Welcome is : 0 First Index of good is : 19 First Index of Hello is : -1

3). indexOf(String substring, int char)

This method returns the index position for the given char value and from fromIndex location.

public class StringIndexOfExample3 { public static void main(String[] arr) { String str = new String("Welcome to Java programming.The Java is good programming language."); System.out.println("the String is : " + str); int index1 = str.indexOf("programming", 7); int index2 = str.indexOf("Java", 4); int index3 = str.indexOf("Welcome", 0); int index4 = str.indexOf("good", 12); int index5 = str.indexOf("Hello", 0); System.out.println("\nFirst Index of programming is : " + index1); System.out.println("First Index of java is : " + index2); System.out.println("First Index of Welcome is : " + index3); System.out.println("First Index of good is : " + index4); System.out.println("First Index of Hello is : " + index5); } } output: the String is : Welcome to Java programming.The Java is good programming language. First Index of programming is : 16 First Index of java is : 11 First Index of Welcome is : 0 First Index of good is : 40 First Index of Hello is : -1

4). indexOf(String str)

This method returns the index position for the given str.

Here we are searching for the index of a particular String value in a String object using the indexOf() method. If the searched String is found, its first index is returned, otherwise, if the String is not found then -1 is returned.

public class StringIndexOfExample4 { public static void main(String[] arr) { String str = new String("Welcome to Java programming.The Java is good programming language."); System.out.println("the String is : "+str); int index1 = str.indexOf("programming"); int index2 = str.indexOf("Java"); int index3 = str.indexOf("Welcome"); int index4 = str.indexOf("good"); int index5 = str.indexOf("Hello"); System.out.println("\nFirst Index of programming is : "+ index1); System.out.println("First Index of java is : "+ index2); System.out.println("First Index of Welcome is : "+ index3); System.out.println("First Index of good is : "+ index4); System.out.println("First Index of Hello is : "+ index5); } } output: the String is : Welcome to Java programming.The Java is good programming language. First Index of programming is : 16 First Index of java is : 11 First Index of The is : 28 First Index of good is : 40 First Index of Hello is : -1

Post/Questions related to  String indexOf() method example in Java

How to get the index of the string in java?
How to split a string by position in java?
How to get an index of the string in java?
How to get the last index of the string in java?
How to get the first index of the string in java?
How to find the position of a character in a string in java
How to get the second last index of the string in java?
How to find the character in string in java?

In this article, we have seen String indexOf() method example in Java. All source code in the article can be found in the GitHub repository.