Java Data Type How to - Search the last occurrence of 'a' in the String








Question

We would like to know how to search the last occurrence of 'a' in the String.

Answer

public class MainClass{
//  w w w .j  a  v  a 2 s . co  m
  public static void main(String[] arg){
    String str = "abcdea";
    
    int index = 0;        
    index = str.lastIndexOf('a');         

    System.out.println(index);
  }

}

The code above generates the following result.