字符串搜索-java

来源:互联网 发布:考试app软件 编辑:程序博客网 时间:2024/05/19 16:22
  1. Java源码字符串匹配
static int indexOf(char[] source,char[] target) {        char first = target[0];        int max =  (source.length - target.length);        for (int i =  0; i <= max; i++) {            /* Look for first character. */            if (source[i] != first) {                while (++i <= max && source[i] != first);            }            /* Found first character, now look at the rest of v2 */            if (i <= max) {                int j = i + 1;                int end = j + target.length - 1;                for (int k =  1; j < end && source[j] == target[k]; j++, k++);                if (j == end) {                    /* Found whole string. */                    return i ;                }            }        }        return -1;    }
0 0
原创粉丝点击