Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Number of characters different between 2 strings

Posted on 2004-08-26
9
Medium Priority
?
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 15

Accepted Solution

by:
Colosseo earned 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 400 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

704 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