字符串匹配算法:就是给定两个串,查找另一个串是否在主串里面,在这里,我们讨论的时候主要用字符串来实现。

  • 什么是串?
    由零个或多个字符组成的有序序列:‘abcdef’
  • 串的长度:串中字符的数目成为串的长度
  • 空串:什么都没有;“ ”有空格的叫做空格串
  • 子串:子串包含空串和串本身
  • 子串在主串中的位置:
  • java 字符串 通配符 java 字符串匹配_BF算法和KMP算法

  • 一、BF算法
package com.impl;

/**
 * @program: sadd
 * @description:
 * @author: 
 * @create: 2019-01-09 09:58
 **/
public class TestBFDemo {
    public static int bf(String str,String sub){
        int i = 0;
        int j = 0;
        while(i < str.length()){
            while(j < sub.length()){
                if(str.charAt(i) == sub.charAt(j)){
                    i++;
                    j++;
                }else{
                    i = i-j+1;
                    j =0;
                }
            }
            break;
        }
        return i-j;
    }

    public static void main(String[] args) {
        String str = "ababcabcdabcde";
        String sub = "abcd";
        int n = TestBFDemo.bf(str,sub);
        System.out.println(n);
    }
}