字符串关键字搜索匹配提取处理算法

来源:互联网 发布:淘宝上卖短信轰炸机吗 编辑:程序博客网 时间:2024/05/02 00:01

名称:字符串关键字匹配提取处理算法

功能:两个字符串,提取其中重叠的字符串

语言: Basic

功能:做字符的匹配,可以用做字符搜索

Sub strDeal()
    Dim str1, str2, strdata As String
    Dim i, j, tempi As Integer
   str1 = "中国工人先锋共产党监督委员会"
   str2 = "工人先锋监督特别委员会"
   
   
    i = 1
    j = 1
    tempi = 0
   
   
    '如果溢出,则退出循环
    While i <= Len(str1) And j <= Len(str2)
            '逐个字符提取比较,如果相等,则下标递增,提取相等的字符,不断截断子字符串的已经比较的字符
        If Mid(str1, i, 1) = Mid(str2, j, 1) Then
            strdata = strdata & Mid(str1, i, 1)
            i = i + 1
            j = j + 1
            str2 = Right(str2, Len(str2) - j + 1)
            tempi = 0
            'tempi作为一个标记,记录标号i的原始位置,当遇到相等的字符,tempi清零
        ElseIf 0 = tempi Then
            tempi = i
         '如果母字符串已经溢出,而子字符串没有截取比较完,也就是说子字符串中一定存在母字符串中所没有的字符
        ElseIf i = Len(str1) And j <= Len(str1) Then
            i = tempi
           j = j + 1
        str2 = Right(str2, Len(str2) - j + 1) '截掉不匹配的字符
        Else
            i = i - j + 2  '下标i继续递增
        End If
        j = 1  '初始,下标j指向子字符串的第一个字母

    Wend
   Debug.Print strdata

End Sub

结果打印:工人先锋监督委员会

 

 


 

原创粉丝点击