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:
1 2 3 | 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:
1 | public int lastIndexOf(String str); |
StringBufferLastIndexOfMethod.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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 2 | 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:
1 | public int lastIndexOf(String str, int fromIndex); |
StringBufferLastIndexOfMethod2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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 2 3 4 | 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:
- Java – StringBuffer class
- Java – StringBuffer append() method (13)
- Java – StringBuffer capacity() method
- Java – StringBuffer charAt(int index) method
- Java – StringBuffer delete(int start, int end) method
- Java – StringBuffer deleteCharAt(int index) method
- Java – StringBuffer ensureCapacity(int minimumCapacity) method
- Java – StringBuffer indexOf() method (2)
- Java – StringBuffer insert() method (12)
- Java – StringBuffer lastIndexOf() method (2)
- Java – StringBuffer length() method
- Java – StringBuffer replace(int start, int end, String str) method
- Java – StringBuffer reverse() method
- Java – StringBuffer substring(int start, int end) method
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/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!