查詢字串最後一次出現的位置
阿新 • • 發佈:2019-01-10
以下例項中我們通過字串函式 strOrig.lastIndexOf(Stringname) 來查詢子字串 Stringname 在 strOrig 出現的位置:
例項程式碼如下:
SearchlastString.java 檔案
public class SearchlastString { public static void main(String[] args) { String strOrig = "Hello world ,Hello Runoob"; int lastIndex = strOrig.lastIndexOf("Runoob"); if(lastIndex == - 1){ System.out.println("沒有找到字串 Runoob"); }else{ System.out.println("Runoob 字串最後出現的位置: "+ lastIndex); } } }
以上程式碼例項輸出結果為:
Runoob 字串最後出現的位置: 19