public static void main(String[] args) {
TestIndexof test = new TestIndexof();
System.out.println(test.indexof("woaini", "i"));
}
public int indexof(String parent,String child) {
if(parent==null || child==null||parent.length()<1||child.length()<1) {
return -1;
}
if(child.length()>parent.length()) {
return -1;
}
char[] parentArray = parent.toCharArray();
char[] childArray = child.toCharArray();
int parentLen = parent.length();
int childLen = child.length();
boolean find = false;
for(int i=0; i<parentLen; i++) {
int count = 0;//记录是否与child长度一样,匹配不一样重新赋值
if(parentArray[i]==childArray[0] && i+childLen<=parentLen) {
for(int j=0; j<child.length(); j++) {
//判断是否匹配
if(parentArray[i+j]==childArray[j]) {
count++;
}
if(count==childLen) {
find = true;
}
}
if(find) {
return i;
}
}
}
return -1;
}
indexof
最新推荐文章于 2022-11-14 17:09:23 发布