Sunday, 7 February 2016

Write a program which find the occurrence of substring in string ?

package test;

public class substring {

public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "helloslkhellodjladfjhello";
    String findStr = "hello";
    int lastIndex = 0;
    int count = 0;

    while (lastIndex != -1) {

   
    System.out.println("first "+lastIndex);
    if (lastIndex != -1) {
    count++;
    lastIndex += findStr.length();
   
    }
    }
    System.out.println("number of substring"+count);

}

}

No comments:

Post a Comment