In this article, we will discuss StringBuilder’s lastIndexOf() method which returns last occurrence of specified sub-string
1. StringBuilder’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 –> substring 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 :
- 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.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
Related Articles :
- Java – StringBuilder class
- Java – StringBuilder append() method (13)
- Java – StringBuilder capacity() method
- Java – StringBuilder charAt(int index) method
- Java – StringBuilder delete(int start, int end) method
- Java – StringBuilder deleteCharAt(int index) method
- Java – StringBuilder ensureCapacity(int minimumCapacity) method
- Java – StringBuilder indexOf() method (2)
- Java – StringBuilder insert() method (12)
- Java – StringBuilder lastIndexOf() method (2)
- Java – StringBuilder length() method
- Java – StringBuilder replace(int start, int end, String str) method
- Java – StringBuilder reverse() method
- Java – StringBuilder 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 !!