Saturday, December 26, 2015

IndexOf() and last indexOf()

Searching Strings:

1)IndexOf() = searches for the first occur of a character 
                           int indexOf(int ch);

2)last indexOf()=searches for the last occur of a character 
                              int lastIndexOf(int ch);






package stringdemo;


public class IndexOfAndlastIndexOfDemo {
    
    public static void main(String[] args) {
        String s="I am a bad guy";
        
        System.out.println(s);
        System.out.println("indexOf(a)"+s.indexOf('a'));
        System.out.println("lastindexOf(g)"+s.lastIndexOf('a'));
    }
    
}
Output:
I am a bad guy
indexOf(a)2
lastindexOf(g)8

0 comments:

Post a Comment