Tìm kiếm vị trí cuối cùng của String trong Java



Miêu tả vấn đề

Cách tìm kiếm vị trí cuối cùng của Chuỗi phụ trong Java?

Giải pháp

Ví dụ sau minh họa cách tìm kiếm vị trí cuối cùng của Chuỗi phụ bên trong một String bởi sử dụng phương thức strOrig.lastIndexOf(Stringname) trong 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);
      }
   }
}

Kết quả

Code trên sẽ cho kết quả sau:

Last occurrence of Hello is at index 13

string_trong_java.jsp