Program to find first position of a String in another string

Hi , My today's post is on a program to find  Position of a String in another String.
For Example ,Given two strings, A and B, how to find the first position of B in A? For instance, A = " ab123cdefgcde"; B= "cde" Then the first position of B in A is 6.

 import java.util.Scanner;

public class SB

 public static void main(String[] args) {

   String test=null;
   String index=null;
   int j=0;
   System.out.println("Enter the string");
   test= new Scanner(;
   System.out.println("Enter the substring you want to count");
   index= new Scanner(;
   for (int i = 0; i <= test.length()-index.length(); i++) {
    String testing=test.substring(i, index.length()+i);
    System.out.println("It occurs first  at "+ j +" position" );


Post a Comment