Java Examples - Find the Last Occurrence Position of a String
In the following example, we use the string function strOrig.lastIndexOf(Stringname) to find the position where the substring Stringname appears in strOrig:
The example code is as follows:
SearchlastString.java file
public class SearchlastString {
public static void main(String[] args) {
String strOrig = "Hello world ,Hello Example";
int lastIndex = strOrig.lastIndexOf("Example");
if(lastIndex == - 1){
System.out.println("The string Example was not found");
}else{
System.out.println("The last occurrence position of the string Example:"+ lastIndex);
}
}
}
The output of the above code example is:
Example 字符串最后出现的位置: 19Other Extensions
Java Examples