String lastIndexOf()

This string lastindexof() java is used to get the last index of the specified character. You can get the last index of any character or substring from a String. If the given character or substring is doesn’t exist in the current string, then it returns -1. The return type of lastIndexOf() is int. It always returns an integer number. The lastIndexOf() is an overloaded method in the String class. Let’s see all the overload String lastIndexOf() Java.

  1. lastIndexOf(int characterName);
  2. lastIndexOf(int characterName, int fromIndex);
  3. lastIndexOf(String subStringName);
  4. lastIndexOf(String subStringName, int fromIndex);
String lastIndexOf Java

1. lastIndexOf(int characterName): This method accepts only one parameter. It returns the last index value of the specified character.

Important point: As you can see this method can accept the int parameter, but we can give it character also because it expects to encode the codepoint.

stringName.lastIndexOf(int characterName)

stringName: The name of string in which you want to find the index of a given character.
characterName: Give the character whom position you want to find in the current string.

class ExampleOfLastIndexOf
 {  
	 public static void main(String args[])
	 {  
		String name = "JAVA WINGS";  
		int index = name.lastIndexOf('A');
		System.out.println("The last index of given character is = "+index);	
          }
}  

Output: The last index of given character is = 3

2. lastIndexOf(int characterName, int fromIndex): This method accepts two parameters. One parameter is the name of the character and another the index to start the search from. It returns the last index value of the specified character.

stringName.lastIndexOf(int characterName, int fromIndex)

stringName: The name of string in which you want to find the index of a given character.
characterName: Give the character whom position you want to find in the current string.
fromIndex: the index to start the search from.

class ExampleOfLastIndexOf
 {  
	 public static void main(String args[])
	 {  
		String name = "JAVA WINGS";  
		int index = name.lastIndexOf('A');
		System.out.println("The last index of given character is = "+index);
					 
		int index1 = name.indexOf('A', 4);
                System.out.println("The last index of character after the specified index 
                for search = "+index1);
         }
}  

Output: The last index of given character is = 3
The last index of character after the specified index for search = -1

3. lastIndexOf(String subStringName): This method accepts only one parameter. It returns the last index value of the first occurrence of the specified substring.

stringName.lastIndexOf(String subStringName)

stringName: The name of string in which you want to find the index of the given substring.
subStringName: Give the subString whom position you want to find in the current string.

class ExampleOfLastIndexOf
 {  
	 public static void main(String args[])
	 {  
		String name = "JAVA WINGS";  
		int index = name.lastIndexOf("WING");
		System.out.println("The last index of given subString is = "+index);	
         }
}  

Output: The last index of given subString is = 5

4. lastIndexOf(String subStringName, int fromIndex): This method accepts two parameters. One parameter is the name of substring and another the index to start the search from. It returns the last index value of the specified substring.

stringName.lastIndexOf(String subString, int fromIndex)

stringName: The name of string in which you want to find the index of the given substring.
subStringName: Give the substring whom position you want to find in the current string.
fromIndex: the index to start the search from.

class ExampleOfIndexOf
 {  
	 public static void main(String args[])
	 {  
		String name = "JAVA WINGS";  
		int index = name.lastIndexOf("WING", 2);
		System.out.println("The last index of given subString is = "+index);
	}
}  

Output: The last index of given subString is = -1

Leave a Comment