TWCMIL
asked on
Number of characters different between 2 strings
I'm looking for a function (built-in or user) that will spit back the amount of characters as an integer that are different between 2 strings. It must be position and length sensitive, meaning that if there is an "E" at position 2 in string one, and an "E" at position 4 in string two, that is considered different. If either string is longer than the other, then the longer of the 2 strings must consider the "extra" characters as mismatches. It should be case insensitive. This function has to be extremely quick as it's going to be called about 2500 times in a single operation I have.
The output should be as follows (assuming the function name is strDiff):
strDiff("resonate","resona te") 'returns 0 - exact match
strDiff("hello","hi") 'returns 4 - only the first h is a match, the "ello" has 4 characters that are not matched
strDiff("blue","yellow") 'returns 6 - yellow has 6 characters and blue does not have any in the same position
strDiff("horse","pony") 'returns 4 - only the second O is a match
strDiff("james","jimmy") 'returns 3 - the first J and third M characters are matches, 3 characters are not matched
strDiff("swinging","swing" ) 'returns 3 - "ing" has no match
The output should be as follows (assuming the function name is strDiff):
strDiff("resonate","resona
strDiff("hello","hi") 'returns 4 - only the first h is a match, the "ello" has 4 characters that are not matched
strDiff("blue","yellow") 'returns 6 - yellow has 6 characters and blue does not have any in the same position
strDiff("horse","pony") 'returns 4 - only the second O is a match
strDiff("james","jimmy") 'returns 3 - the first J and third M characters are matches, 3 characters are not matched
strDiff("swinging","swing"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh yeah, I forgot to take out the commented string
'strDiff = Abs(Len(str1) - Len(str2))
Didn't need it
This better not be homework
'strDiff = Abs(Len(str1) - Len(str2))
Didn't need it
This better not be homework
and another variant you can opt for:
Function strDiff(str1 As String, str2 As String) As Long
Dim counter As Long
Dim i As Integer
counter = Abs(Len(str1) - Len(str2))
Select Case (Len(str1) - Len(str2))
Case = 0
For i = 1 To Len(str1)
If Mid(str1, i, 1) <> Mid(str2, i, 1) Then counter = counter + 1
Next i
Case < 0
For i = 1 To Len(str1)
If Mid(str1, i, 1) <> Mid(str2, i, 1) Then counter = counter + 1
Next i
Case > 0
For i = 1 To Len(str2)
If Mid(str1, i, 1) <> Mid(str2, i, 1) Then counter = counter + 1
Next i
End Select
strDiff = counter
End Function
enjoy!
Function strDiff(str1 As String, str2 As String) As Long
Dim counter As Long
Dim i As Integer
counter = Abs(Len(str1) - Len(str2))
Select Case (Len(str1) - Len(str2))
Case = 0
For i = 1 To Len(str1)
If Mid(str1, i, 1) <> Mid(str2, i, 1) Then counter = counter + 1
Next i
Case < 0
For i = 1 To Len(str1)
If Mid(str1, i, 1) <> Mid(str2, i, 1) Then counter = counter + 1
Next i
Case > 0
For i = 1 To Len(str2)
If Mid(str1, i, 1) <> Mid(str2, i, 1) Then counter = counter + 1
Next i
End Select
strDiff = counter
End Function
enjoy!
P.S. you can cut down on another variable by replacing counter with the function name. you won't need the variable counter anymore.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
absolutely nice & clean!
Thanks!
ASKER
Thanks for all the quick help, I want to give everyone points!
Points to Colloseo for being first, and to BlueDevilFan for having the shortest code.
Thanks again (No it's not homework, it is for some phonetic matching I am doing for our intranet people search site between 2 names converted to double metaphone - we have some funky last names here)
Points to Colloseo for being first, and to BlueDevilFan for having the shortest code.
Thanks again (No it's not homework, it is for some phonetic matching I am doing for our intranet people search site between 2 names converted to double metaphone - we have some funky last names here)
Function strDiff(str1 As String, str2 As String) As Integer
Dim x As Integer, longString As String
If str1 = str2 Then
strDiff = 0
Exit Function
End If
If str1 <> str2 Then 'get the longest of the two
If Len(str1) > Len(str2) Then
longString = str1
Else
longString = str2
End If
'strDiff = Abs(Len(str1) - Len(str2))
For x = 1 To Len(longString) 'compare the two strings character by character
If Mid(str1, x, 1) <> Mid(str2, x, 1) Then
strDiff = strDiff + 1
End If
Next x
End If
End Function