Java IndexOf() method

    IndexOf() method

    This method will allow you to provide the index value of the specified string value or the substring. The index will start from zero, and if it is not found then, -1 will be returned as a result.

    Method implementation-

    public int indexOf(int ch) {  
    
            return indexOf(ch, 0);  
    
        }  

    Syntax-

    int indexOf(int ch)

    It will return the index position for the given char value

    int indexOf(int ch, int fromIndex)

    It will return the index position for the given char value and from the index

    int indexOf(String substring)

    It will return the index position for the given substring

    int indexOf(String substring, int fromIndex)

    It will return the index position for the given substring and from the index.

    Example-

    public class Simple{  
    
        public static void main(String[] args) 
        {      
           String s1 = "Welcome Java";          
    
           int i1=s1.indexOf("come");
    
           System.out.println(i1);
    
           int i2=s1.indexOf("is",4);
    
           System.out.println(i2);     
        }
    }  

    Output-

    Java Indexof Method

    Example-

    public class Simple{  
        public static void main(String[] args)
        {      
           String s1 = "Welcome Java";          
           int i1=s1.indexOf("Java");
           System.out.println(i1);
        }
    }  

    Output-

    Java index of method

    Example-

    public class Simple{  
       public static void main(String[] args) {      
    
           String s1 = "Welcome Java";          
           int i1=s1.indexOf("Java",3);
           System.out.println(i1);
       }
    }  

    Output-

    Java indexof start from