Java中查找字符串最后一次出现的位置
方法:strOrig.lastIndexOf(StringName)来查找子字符串StringName在strOrig出现的位置
public class SearchlastString {
public static void main(String[] args) {
String strOrig = "Hello world ,hello reader";
String str = "hello";
System.out.println(strOrig);
int lastIndex = strOrig.lastIndexOf(str);
System.out.println("查找"+str+"最后一次出现的位置。");
if (lastIndex ==-1) {
System.out.println("该字符串中没有找到指定字符串");
} else{
System.out.println("指定字符串最后一次出现的位置在: "+lastIndex);
}
}
结果: