java.lang.StringBuilder lastIndexOf(String str) and lastIndexOf(String str, int fromIndex)

Description

On this document we will be showing a java example on how to use the lastIndexOf() method of StringBuilder Class. Basically the lastIndexOf method search on the StringBuilder character sequence for the last occurrence of the specified method argument String. That’s basically the function of the lastIndexOf method, but take into consideration that it is overloaded. One of the method accepts only a String method argument, while the second one accepts a String input with a new argument fromIndex. These two methods are basically the same, it’s just that the method with argument fromIndex, starts searching for the String on the character sequence based on the fromIndex input.

Notes:

  • lastIndexOf(str) is equal to lastIndexOf(str,0)
  • Method overloading is one of the feature of java where we can use the same method name but with different method signature.
  • The lastIndexOf method is similar to indexOf method, but the lastIndexOf will returned the last occurence while the indexOf() method returns the first occurrence.

Method Syntax

1.

public int lastIndexOf(String str)

2.

public int lastIndexOf(String str, int fromIndex)

Method Argument

Data Type Parameter Description
String str any String we want to search on our character sequence
int fromIndex the index to start the search from.

Method Returns

The indexOf method returns the index within this string of the last occurrence of the specified substring.. If the search string has not been found, -1 will be returned.

Compatibility

Requires Java 1.5 and up

Java StringBuilder lastIndexOf() Example

Below is a java code demonstrates the use of lastIndexOf() method of StringBuilder class. The example presented might be simple however it shows the behavior of the lastIndexOf() method.

package com.javatutorialhq.java.examples;

/*
 * A java example source code to demonstrate
 * the use of lastIndexOf() method of StringBuilder class
 */

public class StringBuilderLastIndexOfExample {

	public static void main(String[] args) {

		// initialize the StringBuffer object
		StringBuilder sb = new StringBuilder("javatutorialhq.com");
		System.out.println("Contents of buffer:" + sb);

		// get the index of "a" string starting from index 10 
		// from the StringBuilder object
		String str = "a";
		int fromIndex = 1;
		int index = sb.lastIndexOf(str, fromIndex);
		System.out.println("index:" + index);
		
		/*
		 * incorporate substring method. Get a part of StringBuilder 
		 * from the result of lastIndexOf 
		 * lookup until the end of this StringBuilder 
		 * */
		String result = sb.substring(index,sb.length());
		System.out.println("Result:" + result);

	}
}

The above java example source code demonstrates the use of lastIndexOf() method of StringBuilder class. Initially the code assigns a string “javatutorialhq.com” as initial contents of the StringBuilder. Then we use the lastIndexof method to get the last index of string “a”. This is very useful method that works well with substring method of StringBuilder class. On this example we will also learn how to use the indexof method to work with substring.

Sample Output

Below is the sample output when you run the above example.

StringBuilder lastIndexOf() example output