In this article, we will discuss different variants of last index of methods to get last occurrence of character/substring using String’s lastIndexOf() method
1. String’s lastIndexOf() method:
- This String method is used to get last index of the specified character/substring from invoking string
- Note: There are 4 variants or overloaded lastIndexOf() methods
1.1 Method Signature:
public int lastIndexOf(int ch);
public int lastIndexOf(int ch, int fromIndex);
public int lastIndexOf(String str);
public int lastIndexOf(String str, int fromIndex);
1.2 Parameters:
- ch –> character to be searched, to get last occurrence (it’s for single char)
- fromIndex –> position from where to start searching
- str –> substring to be searched, to get last occurrence (it’s for substring)
1.3 Returns:
lastIndexOf() method |
Returns |
lastIndexOf(int ch); | Returns last occurrence of specified character |
lastIndexOf(int ch, int fromIndex); | Returns last occurrence of specified character, starting from specified index |
lastIndexOf(String str); | Returns last occurrence of specified substring |
lastIndexOf(String str, int fromIndex); | Returns last occurrence of specified substring, starting from specified index |
2. Examples on lastIndexOf() method:
- Sample Java program to get last occurrence of specified character/substring using String’s lastIndexOf() method
StringLastIndexOfMethod.java
package in.bench.resources.string.methods;
public class StringLastIndexOfMethod {
/**
* lastIndexOf() - starts searching from backward
* prints index-position from Left-to-Right
* it is similar to IndexOf() method
* @param args
*/
public static void main(String[] args) {
// sample string
String url = "BenchResource.Net";
System.out.println("lastIndexOf() method - "
+ "starts searching from backward\n"
+ "prints index-position from left-to-right "
+ "- similar to IndexOf()\n\n");
// 1 - to get LAST index of char 'e'
int lastIndexOfCh = url.lastIndexOf('e');
// printing to console
System.out.println("Last index of char 'e' is : "
+ lastIndexOfCh);
// 2 - to get LAST index of char 'c',
// starting from specified position
int lastIndexOfChFrom = url.lastIndexOf('c', 13);
// printing to console
System.out.println("Last index of char 'c', "
+ "starting from 13th position is : "
+ lastIndexOfChFrom);
// 3 - to get LAST index of substring 'Resource'
int lastIndexOfSubstring = url.lastIndexOf("Resource");
// printing to console
System.out.println("Last index of substring "
+ "'Resource' is : " + lastIndexOfSubstring);
// 4 - to get LAST index of substring 'sour',
// starting from specified position
int lastIndexOfSubstringFrom = url
.lastIndexOf("sour", 12);
// printing to console
System.out.println("Last index of substring 'sour', "
+ "starting from 12th position is : "
+ lastIndexOfSubstringFrom);
}
}
Output:
lastIndexOf() method - starts searching from backward
prints index-position from left-to-right - similar to IndexOf()
Last index of char 'e' is : 15
Last index of char 'c', starting from 13th position is : 11
Last index of substring 'Resource' is : 5
Last index of substring 'sour', starting from 12th position is : 7
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
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 !!