vb中有没有比较两个相同长度的字符串,返回这两个字符串有几位不同的函数
如123456
  213456
函数能够返回2

解决方案 »

  1.   

    自己写吧
    Public Function comparestr(ByVal str1$, ByVal str2$) As Long
        Dim ii&, ilen&, icount&
        ilen = Len(str1)
        If ilen <> Len(str2) Then
            comparestr = -1
            Exit Function
        End If
        
        icount = 0
        For ii = 1 To ilen
            icount = icount + (Mid(str1, ii, 1) <> Mid(str2, ii, 1))
        Next
        comparestr = -icount
    End Function
      

  2.   

    Private Function GetDiffStrCount(srcStr1 As String, srcStr2 As String) As Long
        Dim StrLen As Long
        Dim TempStr As String
        Dim I As Long
        Dim intDiffCount As Long
        
        '两个字符串长度相同
        StrLen = Len(srcStr1)
        
        intDiffCount = 0
        
        For I = 1 To StrLen
            If Mid(srcStr1, I, 1) <> Mid(srcStr2, I, 1) Then
                intDiffCount = intDiffCount + 1
            End If
        Next I
        
        GetDiffStrCount = intDiffCount
        
    End Function'调用函数 GetDiffStrCount
    Private Sub cmdGetCount_Click()
        MsgBox GetDiffStrCount("123456", "213456")
    End Sub