在类中编写一个方法,在另一个字符串搜索特定的字符串

img


在类中编写一个方法,在另一个字符串搜索特定的字符串,如果前者存在于后一个字符串中,则该方法必须返回true


public class SearchString { 
    public static boolean isExist(String s1, String s2){ 
        if(s1 == null || s1.length() == 0 || s2 == null || s2.length() == 0){ 
            return false; 
        } 
        for(int i = 0; i < s2.length(); i++){ 
            if (i+s1.length() > s2.length())
                return false;
            if(s2.substring(i, i+s1.length()).equals(s1))
                return true;
        } 
        return false; 
    } 
}

实现思路:
1.比较两个字符串的长度,若前者的长度大于后者,则直接返回false
2.循环遍历后者,若存在字符和前者的第一个字符相同,则循环遍历前者
3.用一个变量存放前者和后者字符连续相同的个数,若个数和前者的长度相等,则返回true

img