求子串位置的定位函数

package test1;
public class test2 {
	public static int index(char[] s,char[] t,int pos) {
		int i=pos;
		int j=0;
		while (i<s.length && j<t.length) {					
			if (s[i]==t[j]) {
				++i;
				++j;
			}
			else {
				i=i-j+1;
				j=0;
			}
		}
		if (j>=t.length) {
			return i-t.length;
		}
		else return 0;
	}
	public static void main(String[] args) {
		String S="asdffwrewrew";
		char[] s1=S.toCharArray();
		String T="ew";
		char[] t1=T.toCharArray();
		System.out.println(index(s1, t1, 0));
	}
}

  

posted @ 2014-05-21 10:20  蓦然回首的包子  阅读(859)  评论(0)    收藏  举报