Mega Code Archive

 
Categories / Java Tutorial / Data Type
 

Use String indexOf to locate a substring in a string

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