Following example we are going to see about “how to find the last occurrence position of substring inside string using lastIndexOf(String str) method in Java.
Sample Program
package com.javatutorialcorner.javastring;
public class LastOccurenceofString {
public static void main(String[] args) {
String jtc = "Java tutorials corner contains tutorials for Java programers day to day needs tutorials like Java, Spring, Hibernate, String, JSON, Jackson, GSON, XML parsing, Web Service, SOAP Web Service, RESTful Web Service";
String subString = "Java";
int index = jtc.lastIndexOf(subString);
if (index == -1) {
System.out.println(subString + " not found");
} else {
System.out.println("Last Occurence of " + subString
+ " found at index " + index);
}
}
}
Output
The above code produce following output:
Last Occurrence of Java found at index 93
0 comments:
Post a Comment