Java 實(shí)例 - 查找字符串最后一次出現(xiàn)的位置
以下實(shí)例中我們通過(guò)字符串函數(shù) strOrig.lastIndexOf(Stringname) 來(lái)查找子字符串 Stringname 在 strOrig 出現(xiàn)的位置:
實(shí)例代碼如下:
//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); } } }
以上代碼實(shí)例輸出結(jié)果為:
Last occurrence of Hello is at index 13
更多建議: