How to get the index of the last letters of a String using lastIndexOf() in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            String s = "java";

            int i = s.lastIndexOf('a');
            System.out.println(i);

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:
       
3
    
 */

 



answered Nov 23, 2016 by avibootz
0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            String s = "java";

            int i = s.lastIndexOf('x');
            System.out.println(i);

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:
       
-1
    
 */

 



answered Nov 23, 2016 by avibootz
...