package com.william_wei.interview.other;
/**
* @Describe
* @Author: william_xhw
* @Version 1.0
*/
public class MatchStringJava {
private Integer base = 0;
private String str_s;
private String str_t;
public MatchStringJava(String str_s, String str_t) {
this.str_s = str_s;
this.str_t = str_t;
}
public Integer run() {
int sourceLength = str_s.length();
int targetLength = str_t.length();
// 长度匹配
while (base + targetLength <= sourceLength) {
int step = 0;
while (step < targetLength) {
//目标串的步长位置值 与 源字符串对应位置 比较
if (str_t.charAt(step) == str_s.charAt(base + step)) {
step += 1;
continue;
}
//如果不匹配,基准位置递增1
base += 1;
break;
}
//步长是否等于目标字符串的长度.
if (step == targetLength) {
return base;
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(new MatchStringJava("badfsdfd", "sdf").run());
//输出结果为4
}
}