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","resonate") '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
LVL 1
TWCMILAsked:
Who is Participating?
 
ColosseoConnect With a Mentor Commented:
Hi there

give this function a go, hope its what your after

Private Function str_Diff(str_One As String, str_Two As String)

  Dim int_Shortest
 
  ' Set the return value to 0
  str_Diff = 0
 
  ' If the strings length match then
  If Len(str_One) = Len(str_Two) Then
    ' For each character
    For i = 1 To Len(str_One)
      ' If the character does not match add 1 to the return value
      If Mid(str_One, i, 1) <> Mid(str_Two, i, 1) Then
        str_Diff = str_Diff + 1
      End If
    Next
  ' If the strings do not match
  Else
    ' Get the length of the shortest string and add the difference in length to the return value
    If Len(str_One) > Len(str_Two) Then
      int_Shortest = Len(str_Two)
      str_Diff = Len(str_One) - Len(str_Two)
    Else
      int_Shortest = Len(str_One)
      str_Diff = Len(str_Two) - Len(str_One)
    End If
   
    ' Compare up to the length of the shortest string
    For i = 1 To int_Shortest
      ' If the character does not match add 1 to the return value
      If Mid(str_One, i, 1) <> Mid(str_Two, i, 1) Then
        str_Diff = str_Diff + 1
      End If
    Next
  End If

End Function

Cheers

Scott
0
 
dds110Commented:
Here's a variant of the same thing almost.

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
0
 
dds110Commented:
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
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
PePiCommented:
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!


0
 
PePiCommented:
P.S. you can cut down on another variable by replacing counter with the function name. you won't need the variable counter anymore.

0
 
David LeeConnect With a Mentor Commented:
Is there room for one more alternative?  Here's mine.  I took a somewhat different approach.

Private Function StrDiff(strOne As String, strTwo As String) As Integer
    StrDiff = Abs(Len(strOne) - Len(strTwo))
    For intcounter = 1 To IIf(Len(strOne) <= Len(strTwo), Len(strOne), Len(strTwo))
        StrDiff = StrDiff + IIf(Mid(strOne, intcounter, 1) = Mid(strTwo, intcounter, 1), 0, 1)
    Next
End Function

Here's the logic I used.

1.  Calculate the difference in length between the two strings.  The absolute value of that value is going to go toward the difference without regard to how the rest of the characters match up.

2.  Figure out which string is shorter and go through the loop only that many times.  Any characters in the longer string have already been accounted for in #1 above.

I tested this against the examples provided and they all calculated correctly.  The program might be faster and neater if I just calculated the length of each string up front and didn't make multiple calls to the Len() function.  
0
 
PePiCommented:
absolutely nice & clean!
0
 
David LeeCommented:
Thanks!
0
 
TWCMILAuthor Commented:
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)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.