Mega Code Archive

 
Categories / Java Tutorial / Data Type
 

Use lastIndexOf to find a substring in a string

public class MainClass {    public static void main( String args[] )    {       String letters = "abcdefghijklmabcdefghijklm";       System.out.printf( "Last \"def\" is located at index %d\n",           letters.lastIndexOf( "def" ) );        System.out.printf( "Last \"def\" is located at index %d\n",           letters.lastIndexOf( "def", 25 ) );        System.out.printf( "Last \"hello\" is located at index %d\n",           letters.lastIndexOf( "hello" ) );    } } Last "def" is located at index 16 Last "def" is located at index 16 Last "hello" is located at index -1