Solved

Number of characters different between 2 strings

Posted on 2004-08-26
9
292 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:TWCMIL
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 15

Accepted Solution

by:
Colosseo earned 100 total points
ID: 11903338
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
 
LVL 8

Expert Comment

by:dds110
ID: 11903356
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
 
LVL 8

Expert Comment

by:dds110
ID: 11903395
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
 
LVL 6

Expert Comment

by:PePi
ID: 11907837
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 6

Expert Comment

by:PePi
ID: 11907857
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
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 100 total points
ID: 11909574
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
 
LVL 6

Expert Comment

by:PePi
ID: 11944707
absolutely nice & clean!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 11945026
Thanks!
0
 
LVL 1

Author Comment

by:TWCMIL
ID: 11946303
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now