Java: StringBuilder lastIndexOf() method

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

 

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

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 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

Throws:

  • NullPointerException, if specified sub-string is null

 

Examples on lastIndexOf() method:

1. To find last occurrence of specified sub-string

  • Sample Java program to get last occurrence of specified sub-string

StringBuilderLastIndexOfMethod.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderLastIndexOfMethod {

	/**
	 * 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) {

		// StringBuilder
		StringBuilder sb = new StringBuilder(
				"North is North and South is South");

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

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


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

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

Output:

1. Last index of substring 'North' is : 9
2. Last index of substring 'South' is : 28

 

2. To find last occurrence of specified substring starting from specified index

  • Sample Java program to get last occurrence of specified sub-string from specified start-index or start index-position

StringBuilderLastIndexOfMethod2.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderLastIndexOfMethod2 {

	/**
	 * 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) {

		// StringBuilder
		StringBuilder sb = new StringBuilder(
				"North is North and South is South");

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

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


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

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

Output:

1. Last index of substring 'North', 
								starting from 8th position is  : 9
2. Last index of substring 'South', 
							   starting from 21st position is : 28

 

References:

 

Happy Coding !!
Happy Learning !!

Java: StringBuilder length() method
Java: StringBuilder insert() method