public class PatternMatch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PatternMatch pm=new PatternMatch();
String s="ababcabcacbab";
String pattern="abcac";
pm.match(s, pattern);
}
//字符串匹配算法
/*
* s 是字符串
* pattern 是模式,要匹配的串
*/
public void match(String s,String pattern){
int i=0,j=0;
while(i<s.length()&&j<pattern.length()){
if(s.charAt(i)==pattern.charAt(j)){
i++;
j++;
}else{
i=i-j+1;
j=0;
}
}
if(j==pattern.length()){
System.out.println("匹配成功!位置为:"+(i-j));
}else{
System.out.println("匹配不成功!");
}
}
}