您的位置:首页 > 其它

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

2011-09-01 08:42 477 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐