How to find the the last occurrence of a substring in a string with Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String s = "c++ c php java golang java nodejs";
 
        int pos = s.lastIndexOf("java");
         
        if (pos != -1)
            System.out.println(pos + " " + s.charAt(pos));
        else
            System.out.println("not found");
    }
}


/*
run:

22 j

*/

 



answered Feb 21, 2020 by avibootz
...