通过字符串函数 strOrig.lastIndexOf(Stringname) 来查找子字符串 Stringname 在 strOrig 出现的位置:
public class SearchlastString {
public static void main(String[] args) {
String strOrig = "Hello world ,Hello 周杰伦就是神";
String Str2 = "Hello" ;
int lastIndex = strOrig.lastIndexOf("周杰伦");
int lastIndex2 = strOrig.lastIndexOf("神");
int lastIndex3 = strOrig.indexOf("Hello");
if(lastIndex == - 1){
System.out.println("没有找到字符串 周杰伦");
}else{
System.out.println("周杰伦 字符串最后出现的位置: "+ lastIndex);
}
if(lastIndex2 == - 1){
System.out.println("没有找到字符串 神");
}else{
System.out.println("神 字符串最后出现的位置: "+ lastIndex2);
}
if(lastIndex3 == - 1){
System.out.println("没有找到字符串 Hello");
}else{
System.out.println("Hello 字符串第一次出现的位置: "+ lastIndex3);
System.out.println("从第2个位置开始,Hello第一次的位置:" + strOrig.indexOf( "n", 2 ) ) ;
System.out.println("字符串Str2【"+ Str2 +"】最后的位置:" + strOrig.lastIndexOf( Str2 ) ) ;
}
}
}