Showing posts with label match string. Show all posts
Showing posts with label match string. Show all posts

Sunday, December 22, 2013

Find if a substring is exist in a given string !!

public class MatchString {
 
    public int matchStr(String str, String sub){
     
        if(str.length() < sub.length())
            return -1;
     
        for(int i=0; i<str.length() && (str.length()-i >= sub.length()); i++){
            String temp = str.substring(i, i+sub.length());
            //System.out.println("temp: " + temp);
            if(temp.equalsIgnoreCase(sub))
                return 1;
        }
     
        return -1;
    }
 
    public static void main(String args[]){
     
        MatchString ms = new MatchString();
        int res = ms.matchStr("abcdefgh", "cdfe");
        if(res < 0)
            System.out.println("Not Found!");
        else
            System.out.println("Match!");
    }
}