Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Percentage match in string comparison 2

Status
Not open for further replies.

dswitzer

Technical User
Aug 2, 2002
298
US
Hey,

Does anyone have any suggestions to solve an issue on comparing strings:

str1 = "1234567890"
str2 = "123 567890"

These strings match 90% on character and placement -- that is easy to see. Is it easy to code and I have just spent too much time thinking about it?

Thanks.
 
See if this works. It should tell you how many characters are equal and in the same position in s1 and s2.

Code:
Function PercentageMatch(s1 As String, s2 As String)
    Dim MaxChar As Integer
    Dim MinChar As Integer
    If Len(s1) > Len(s2) Then
        MaxChar = Len(s1)
        MinChar = Len(s2)
    Else
        MinChar = Len(s1)
        MaxChar = Len(s2)
    End If
    
    Dim i As Integer
    Dim matches As Integer
    matches = 0
    For i = 1 To MinChar
        If Mid(s1, i, 1) = Mid(s2, i, 1) Then
            matches = matches + 1
        End If
    Next i
    
    PercentageMatch = (matches / MaxChar)
    
End Function
 
Very nice Mr Fault
Have another star - reckon that function's at least worthy of a double star
Rgds
~Geoff~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top