字符串匹配算法

public static void main(String[] args) {
        System.out.println(match("abcdefg", "hfg"));

    }

    public static boolean match(String str, String target) {
        if (str == null || target == null || "".equals(str) || "".equals(target)) {
            return false;
        }
        if (str.length() < target.length()) {
            return false;
        }
        if (str.length() == target.length() && str.equals(target)) {
            return true;
        }
        char[] strArr = str.toCharArray();
        char[] tarArr = target.toCharArray();
        int index = 0;
        while (index <= strArr.length - tarArr.length) {
            A: for (int i = 0; i < tarArr.length; i++) {// abcd
                                                        // bce
                if (strArr[i + index] != tarArr[i]) {
                    index++;
                    break A;
                }
                if (i == tarArr.length - 1) {
                    return true;
                }
            }
        }
        return false;
    }

 

posted @ 2017-03-17 00:36  我_会飞的鱼  阅读(176)  评论(0编辑  收藏  举报