Java – String lastIndexOf() method

In this article, we will discuss different variants of last index of methods to get last occurrence of character/substring using String’s lastIndexOf() method

1. String’s lastIndexOf() method:

  • This String method is used to get last index of the specified character/substring from invoking string
  • Note: There are 4 variants or overloaded lastIndexOf() methods

1.1 Method Signature:

public int lastIndexOf(int ch);
public int lastIndexOf(int ch, int fromIndex);

public int lastIndexOf(String str);
public int lastIndexOf(String str, int fromIndex);

1.2 Parameters:

  • ch                 –> character to be searched, to get last occurrence (it’s for single char)
  • fromIndex  –> position from where to start searching
  • str                 –> substring to be searched, to get last occurrence (it’s for substring)

1.3 Returns:

 lastIndexOf() method

Returns

lastIndexOf(int ch);Returns last occurrence of specified character
lastIndexOf(int ch, int fromIndex);Returns last occurrence of specified character, starting from specified index
lastIndexOf(String str);Returns last occurrence of specified substring
lastIndexOf(String str, int fromIndex);Returns last occurrence of specified substring, starting from specified index

2. Examples on lastIndexOf() method:

  • Sample Java program to get last occurrence of specified character/substring using String’s lastIndexOf() method

StringLastIndexOfMethod.java

package in.bench.resources.string.methods;

public class StringLastIndexOfMethod {

	/**
	 * lastIndexOf() - starts searching from backward
	 * prints index-position from Left-to-Right
	 * it is similar to IndexOf() method
	 * @param args
	 */
	public static void main(String[] args) {

		// sample string
		String url = "BenchResource.Net";

		System.out.println("lastIndexOf() method - "
				+ "starts searching from backward\n"
				+ "prints index-position from left-to-right "
				+ "- similar to IndexOf()\n\n");

		// 1 - to get LAST index of char 'e'
		int lastIndexOfCh = url.lastIndexOf('e');

		// printing to console
		System.out.println("Last index of char 'e' is : "
				+ lastIndexOfCh);

		// 2 - to get LAST index of char 'c',
		// starting from specified position
		int lastIndexOfChFrom = url.lastIndexOf('c', 13);

		// printing to console
		System.out.println("Last index of char 'c', "
				+ "starting from 13th position is : "
				+ lastIndexOfChFrom);

		// 3 - to get LAST index of substring 'Resource'
		int lastIndexOfSubstring = url.lastIndexOf("Resource");

		// printing to console
		System.out.println("Last index of substring "
				+ "'Resource' is : " + lastIndexOfSubstring);

		// 4 - to get LAST index of substring 'sour',
		// starting from specified position
		int lastIndexOfSubstringFrom = url
				.lastIndexOf("sour", 12);

		// printing to console
		System.out.println("Last index of substring 'sour', "
				+ "starting from 12th position is : "
				+ lastIndexOfSubstringFrom);
	}
}

Output:

lastIndexOf() method - starts searching from backward
prints index-position from left-to-right - similar to IndexOf()

Last index of char 'e' is : 15
Last index of char 'c', starting from 13th position is : 11
Last index of substring 'Resource' is : 5
Last index of substring 'sour', starting from 12th position is : 7

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String length() method
Java - String join() method