Java String indexOf() Method with Example

In the last tutorial, we have learned how to find substring in java. We discussed all the two methods to find the substring in java. In this tutorial, we will learn the string indexOf() method with an example. The string class provides four variants of the indexOf() method. Here, we will discuss all the four variants of the Java String indexOf() method. Let's discuss one by one.

Java String indexOf() methods

1. indexOf(int ch): This is the first variant of the Java String indexOf() method. This method returns the index of the first occurrence of the specified character. If the specified character is not present there then it will return -1.

Syntax: The syntax of the first variant of the indexOf() method is given below:

public int indexOf(int ch)
Here, ch is a character whose index we want to return.



Java String indexOf(int ch) Method Example


class StringIndexOf{
	public static void main(String args[]){
		String str = "String tutorial by javastudypoint";
	    //returns index of first
		//occurrence of a.
		System.out.println(str.indexOf('a')); //13
	}
}

Output:


13


2. indexOf(int ch, int fromIndex): This is the second variant of the Java String indexOf() method. This method returns the index of the first occurrence of the specified character, starting the search from the specified index. If the specified character is not present there then it will return -1.

Syntax: The syntax of the second variant of the indexOf() method is given below:


public int indexOf(int ch, int fromIndex)
Here, ch is a character and fromIndex is the index position at which point the search begins.


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


class StringIndexOf{
	public static void main(String args[]){
		String str = "String tutorial by javastudypoint";
	    //returns index of a
		//after 15th index at position.
		System.out.println(str.indexOf('a', 15)); //20
	}
}

Output:

20


3. indexOf(String str): This is the third variant of the Java String indexOf() method. This method returns the index of the first occurrence of the specified substring. If the specified substring is not present there then it will return -1.

Syntax: The syntax of the third variant of the Java String indexOf() method is given below:

public String indexOf(String str)
Here, str is a substring whose index we want to return.


Java String indexOf(String str) Method Example


class StringIndexOf{
	public static void main(String args[]){
		String str = "String tutorial by javastudypoint";
	    //returns the index of 
		//initial character of substring 
		System.out.println(str.indexOf("java")); //19
	}
}

Output:

19


4. indexOf(String str, int fromIndex): This is the fourth variant of the indexOf() method. This method returns the index of the first occurrence of the specified substring, starting the search from the specified index. If the specified substring is not present there then it will return -1.

Syntax: The syntax of the fourth variant of the indexOf() method is given below

public String indexOf(String str, int fromIndex)
Here, str is a substring and fromIndex is the index position at which point the search begins.


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


class StringIndexOf{
	public static void main(String args[]){
		String str = "String tutorial in java by javastudypoint";
	    //returns the index of initial character  
		//of substring after 20th position
		System.out.println(str.indexOf("java", 20)); //27
		
		//returns -1 because specified substring	
		//is not present there. 
		System.out.println(str.indexOf("Java Tutorial", 10)); //-1
	}
}

Output:
27
-1

Java String indexOf() Method with Example Java String indexOf() Method with Example Reviewed by Prashant Srivastava on January 07, 2020 Rating: 5

No comments:

Powered by Blogger.