java中的查找子字符串

来源:互联网 发布:华为云计算认证培训 编辑:程序博客网 时间:2024/05/23 21:00

一个公司出的算法题目,简单实现了下,欢迎指正错误。
查找两个字符串中的子字符串:

public static int getChildStr(String parent,String child){        int pLength = parent.length();        int cLength = child.length();        if(pLength > cLength){            int count = 0;            //找到子child的长度并计算父字符串的可分割数            for(int i = 0;i<(pLength - cLength +1);i++){                int finalIndex = i+cLength-1;                String str = parent.substring(i,finalIndex+1);                if(str.equals(child)){                    count++;                }                //可获取到当前的子字符串起始位置                //System.out.println(i+"  "+finalIndex+" "+count);            }            return count;        }        return 0;    }
0 0