package 字符串下;
/**
* 暴力解决返回第一次匹配的字符串位置
* @author buder_cp
*
*/
public class 模式匹配 {
public static int strStr(String haystack, String needle) {
int m = haystack.length();
int n = needle.length();
for (int i = 0; i < m; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
break;
} else {
count++;
}
}
if (count == n) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int k = strStr("qaz123345234243u68", "1233");
System.out.println(k);
}
}
模式匹配
关注
打赏