Java String lastIndexOf() Method with Example

In the last tutorial, we have learned all about Java String indexOf() method. We discussed all the four variants of the indexOf() method. In this tutorial, we will learn about the Java String lastIndexOf() method. There are four variants of the lastIndexOf() method. Here, we will discuss all the four variants of the Java String lastIndexOf() method. Let's discuss these four variants one by one.
Java String lastIndexOf() Method with Example

1. lastIndexOf(int ch): This is the first variant of Java String lastIndexOf() method. This method returns the index of the last 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 Java String lastIndexOf() method is given below:

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


Java String lastIndexOf(int ch) Method Example


class lastIndexOf{
	public static void main(String args[]){
		String str = "String tutorial by javastudypoint";
	    //returns the last index
		//of specified character.
		System.out.println(str.lastIndexOf('i')); //30
	}
}

Output:

30


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

Syntax: The syntax of the second variant of Java String lastIndexOf() method is given below:

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


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


class lastIndexOf{
	public static void main(String args[]){
		String str = "String tutorial by javastudypoint";
	    //returns the last index
		//of 'i' before 15th index.
		System.out.println(str.lastIndexOf('i', 15)); //12
	}
}

Output:

12


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

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

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


Java String lastIndexOf(String str) Method Example


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

Output:

27


4. lastIndexOf(String str, int fromIndex): This is the fourth variant of Java String lastIndexOf() method. This method returns the index of the first occurrence of the specified substring, starting the search from the backward specified index. This method return -1 if the specified substring is not present there.

Syntax The syntax of the fourth variant of Java String lastIndexOf() method is given below:

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


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


class lastIndexOf{
	public static void main(String args[]){
		String str = "String tutorial in java by javastudypoint";
	    //returns the last index of
		//initial character of substring
		//before index 30.
		System.out.println(str.lastIndexOf("java", 30)); //27
	}
}

Output:

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

No comments:

Powered by Blogger.