Java: StringBuffer lastIndexOf() method

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

 

lastIndexOf() method:

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

 

Method Signature:

public int lastIndexOf(String str);

public int lastIndexOf(String str, int fromIndex);

 

Parameters:

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

 

Returns:

lastIndexOf() method

Returns

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

 

Throws:

  • NullPointerException, if specified substring is null

 

 

Examples on lastIndexOf() method:

Sample Java program to get last occurrence of specified substring using StringBuffer’s lastIndexOf() method

1. To find last occurrence of specified substring

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 - similar to IndexOf()
	 * @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. To find last occurrence of specified substring starting from specified index

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 - similar to IndexOf()
	 * @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

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!

Java: StringBuffer length() method
Java: StringBuffer insert() method