两个字符串比较,取出重复字符个数。 (原创)

Function TwoStrComp(strOne As String, strTwo As String) As Integer
'================================
'取出两个字符串中的重复字符个数
'短字符串中无重复字符的情况下
'================================
    Dim strShort As String
    Dim strLong As String
    Dim strSub As String
    Dim intCount As Integer
    Dim intCyc As Integer
    
    intCount = 0
    
    If strOne <> "" And strTwo <> "" Then
    
        strShort = strOne
        strLong = strTwo
        
        If Len(strOne) > Len(strTwo) Then
        
            strShort = strTwo
            strLong = strOne
            
        End If
        
        For intCyc = 0 To Len(strShort) - 1
        
            strSub = Left(Right(strShort, Len(strShort) - intCyc), 1)
            
            If InStr(strLong, strSub) > 0 Then             'InStr函数返回找到第一个字符的位置
            
                intCount = intCount + 1
                
            End If
            
        Next
        
        TwoStrComp = intCount
        
    End If

End Function

Function TwoStrCompOnly(strOne As String, strTwo As String) As Integer
'================================
'取出两个字符串中的重复字符个数
'重复字符只算1个的情况
'数组法 (也可用collection类)
'================================
    Dim strShort As String
    Dim strLong As String
    Dim strSub As String
    Dim intCount As Integer
    Dim intCyc As Integer
    Dim arrChr() As String
    
    intCount = 0
    
    If strOne <> "" And strTwo <> "" Then
    
        strShort = strOne
        strLong = strTwo
        
        If Len(strOne) > Len(strTwo) Then
        
            strShort = strTwo
            strLong = strOne
            
        End If
        
        ReDim arrChr(Len(strShort))
        
        For intCyc = 0 To Len(strShort) - 1
        
            strSub = Left(Right(strShort, Len(strShort) - intCyc), 1)
            
            If Onlyone(arrChr, strSub) Then
            
                If InStr(strLong, strSub) > 0 Then             'InStr函数返回找到第一个字符的位置
            
                    intCount = intCount + 1
                
                End If
                
            End If
            
            arrChr(intCyc) = strSub
            
        Next
        
        TwoStrCompOnly = intCount
        
    End If

End Function

Function TwoStrCompEx(strOne As String, strTwo As String) As Integer
'================================
'取出两个字符串中的重复字符个数
'任何匹配字符都累加
'================================
    Dim strShort As String
    Dim strLong As String
    Dim strSub As String
    Dim Strtemp As String
    Dim intCount As Integer
    Dim intCyc As Integer
    
    intCount = 0
    
    If strOne <> "" And strTwo <> "" Then
    
        strShort = strOne
        strLong = strTwo
        
        If Len(strOne) > Len(strTwo) Then
        
            strShort = strTwo
            strLong = strOne
            
        End If
        
        For intCyc = 0 To Len(strShort) - 1
        
            strSub = Left(Right(strShort, Len(strShort) - intCyc), 1)
            Strtemp = Replace(strLong, strSub, "")
            intCount = intCount + (Len(strLong) - Len(Strtemp))
                        
        Next
        
        TwoStrCompEx = intCount
        
    End If

End Function


Function TwoStrCompOnlyA(strOne As String, strTwo As String) As Integer
'================================
'取出两个字符串中的重复字符个数
'按LZ要求,第二个字符串有重复,但按1个累加
'数组法 (也可用collection类)
'================================
    Dim strSub As String
    Dim intCount As Integer
    Dim intCyc As Integer
    Dim arrChr() As String
    
    intCount = 0
    
    If strOne <> "" And strTwo <> "" Then
    
        ReDim arrChr(Len(strTwo))
        
            For intCyc = 0 To Len(strTwo) - 1
        
                strSub = Left(Right(strTwo, Len(strTwo) - intCyc), 1)
            
                If Onlyone(arrChr, strSub) Then
            
                    If InStr(strOne, strSub) > 0 Then             'InStr函数返回找到第一个字符的位置
            
                        intCount = intCount + 1
                
                    End If
                
                End If
            
                arrChr(intCyc) = strSub
            
            Next
        
            TwoStrCompOnlyA = intCount

End Function


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值