How to search for substring from a string in JAVA ?

By | June 25, 2011

Hi,

For searching for a particular substring from a starting index of a string and get the starting index of it in JAVA, use the following sample of code.


public class MainClass{

  public static void main(String[] arg){

    String myStr = "coderzco";

    int begIndex= 2;

    int myIndex= 0;

    myIndex = str.indexOf("co", begIndex);

    System.out.println(myIndex);
  }

}

It will print out the starting index of the substring ‘co’ (the second ‘co’), that is 6.

🙂

Leave a Reply

Your email address will not be published. Required fields are marked *