Files
Go-http_framework/util/stringMatch/ViolentMatch.go
T
Huangzj dc5d924a2c feat(Go-Tool):
2021/03/02: 新增字符串匹配 暴力匹配、KMP匹配、SunDay匹配算法
            2021/03/02: 修复Rand包随机获取方法
2021-03-02 13:33:07 +08:00

33 lines
591 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* @Author : huangzj
* @Time : 2021/2/25 11:11
* @Description
*/
package stringMatch
func ViolentMatch(allString, match string) int {
i := 0
j := 0
sLength := len(allString)
mLenght := len(match)
for i < sLength && j < mLenght {
//当前字符如果匹配,继续匹配下一个字符,坐标移动
if allString[i] == match[j] {
j++
i++
} else {
//如果字符不匹配,match从头开始,被匹配的字符串从之前匹配的下一个字符开始,坐标修改
i = i - j + 1
j = 0
}
}
if j == len(match) {
return i - j
}
return -1
}