ตัวอย่างชวา - ค้นหาเกิดขึ้นครั้งล่าสุดของสตริง
ตัวอย่างต่อไปนี้เราฟังก์ชันสตริง strOrig.lastIndexOf (stringname) เพื่อหาตำแหน่งของสตริงย่อยใน strOrig stringname ที่ปรากฏ:
ตัวอย่างของรหัสเป็นดังนี้:
//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