Java – StringBuffer lastIndexOf() method

In this article, we will discuss StringBuffer’s lastIndexOf() method which returns last occurrence of specified sub-string

1. StringBuffer’s lastIndexOf() method:

  • This method is used to get last occurrence of sub-string i.e.; index-position of last occurrence of specified sub-string
  • Note: There are 2 variants or overloaded lastIndexOf() methods

1.1 Method Signature:

public int lastIndexOf(String str);

public int lastIndexOf(String str, int fromIndex);

1.2 Parameters:

  • str                –> sub-string to be searched, to get last occurrence
  • fromIndex  –> position from where to start searching

1.3 Returns:

lastIndexOf() method

Returns

lastIndexOf(String str);Returns last occurrence of specified sub-string (i.e.; index-position for last occurrence)
lastIndexOf(String str, int fromIndex);Returns last occurrence of specified sub-string, starting from specified index (i.e.; index-position for last occurrence)

1.4 Throws:

  • NullPointerException, if specified sub-string is null

2. Examples on lastIndexOf() method:

2.1 To find last occurrence of specified sub-string

  • Below illustration depicts how to get last occurrence of specified sub-string

Method signature:

public int lastIndexOf(String str);

StringBufferLastIndexOfMethod.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferLastIndexOfMethod {

	/**
	 * lastIndexOf() - starts searching from backward
	 * prints index-position from left-to-right
	 * very similar to indexOf() method
	 * @param args
	 */
	public static void main(String[] args) {

		// StringBuffer
		StringBuffer sb = new StringBuffer(
				"East is East and West is West");

		// 1. To get LAST index of substring 'West'
		int lastIndexOfSubstring1 = sb.lastIndexOf("West");

		// print to console
		System.out.println("1. Last index of substring"
				+ " 'West' is : " + lastIndexOfSubstring1);

		// 2. To get LAST index of substring 'East'
		int lastIndexOfSubstring2 = sb.lastIndexOf("East");

		// print to console
		System.out.println("2. Last index of substring"
				+ " 'East' is : " + lastIndexOfSubstring2);
	}
}

Output:

1. Last index of substring 'West' is : 25
2. Last index of substring 'East' is : 8

2.2 To find last occurrence of specified sub-string starting from specified index

  • Below illustration depicts how to get last occurrence of specified sub-string, starting from specified index-position

Method signature:

public int lastIndexOf(String str, int fromIndex);

StringBufferLastIndexOfMethod2.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferLastIndexOfMethod2 {

	/**
	 * lastIndexOf() - starts searching from backward
	 * prints index-position from left-to-right
	 * very similar to indexOf() method
	 * @param args
	 */
	public static void main(String[] args) {

		// StringBuffer
		StringBuffer sb = new StringBuffer(
				"East is East and West is West");

		// 1. To get LAST index of substring 'East',
		// starting from 8th position
		int lastIndexOfSubstring = sb.lastIndexOf("East", 8);

		// print to console
		System.out.println("1. Last index of substring 'East',"
				+ " starting from 8th position is  : "
				+ lastIndexOfSubstring);

		// 2. To get LAST index of substring 'West',
		// starting from 21st position
		int lastIndexOfSubstringFrom = sb.lastIndexOf("West", 21);

		// print to console
		System.out.println("2. Last index of substring 'West', "
				+ "starting from 21st position is : "
				+ lastIndexOfSubstringFrom);
	}
}

Output:

1. Last index of substring 'East',
							starting from 8th position is  : 8
2. Last index of substring 'West',
							starting from 21st position is : 17

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - StringBuffer length() method
Java - StringBuffer insert() method