Java indexOf() & lastIndexOf() Method


The String indexOf() method is used to searches for the first occurrence of a character value. The index count starts from zero.


Syntax:

public int indexOf(int ch)  

Example:

 public class IndexOf{  
     public static void main(String args[])
    {  
        String s1="Welcome to Programtpoint";
        int s2=s1.indexOf('e');
        System.out.println(s2);
    }
} 

Java lastIndexOf() Method

The String lastIndexOf() method is used to searches for the last occurrence of a character value. The index count starts from zero.


Syntax:

public int lastIndexOf(int ch)  

Example:

 public class LastIndexOf
{  
    public static void main(String args[])
    {  
        String s1="Welcome to Onlinetpoint";
        int s2=s1.lastIndexOf('e');
        System.out.println(s2);
    }
}  



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.