Java 實例– 查找字符串最後一次出現的位置
以下實例中我們通過字符串函數strOrig.lastIndexOf(Stringname) 來查找子字符串Stringname 在strOrig 出現的位置:
實例代碼如下:
//SearchlastString.java 文件 public class SearchlastString { public static void main(String[] args) { String strOrig = "Hello world ,Hello Reader"; int lastIndex = strOrig.lastIndexOf("Hello"); if(lastIndex == - 1){ System.out.println("Hello not found"); }else{ System.out.println("Last occurrence of Hello is at index "+ lastIndex); } } }
以上代碼實例輸出結果為:
Last occurrence of Hello is at index 13