Results 1 to 27 of 27

Thread: [RESOLVED] string comparison function

  1. #1

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Resolved [RESOLVED] string comparison function

    Does anyone happen to have a function available with the following functionality:

    Input: 2 strings
    Output: long

    The function should compare the two strings, and return the number of characters that are the same.

    example 1:

    string 1: "123456789012345"
    string 2: "023456789012345"

    Return value: 14

    This was an easy one, and I have no problem writing a function that does that. The problem is, that I want it to be able to recognize patterns that are the same like in the next example.

    example 2:

    string 1: "123456789012345"
    string 2: "234567890123450"

    return value: 14

    I marked the characters that are identical in red:
    123456789012345
    234567890123450

    The purpose of the function is comparing IMEI numbers (an id for mobile phones).
    These imei numbers are always numerical, and have 15 characters.
    I want to find out if the number entered by the customer (when he entered the order on the website), is identical to the number that was on the phone.

    If the numbers are not identical, it could be that the customer made a typo (switched two numbers, or forgot a number, and typed a fake number at the end), or that the number belongs to a different phone.
    I want to handle these situations differently, so if the numbers match partialy (say 12 characters match), I guess the customer made a typo.


    P.S. I know IMEI numbers must pass a luhn check, but there are reasons I don't want to check this at order entry (at least not right now).
    The
    Last edited by Frans C; Jan 12th, 2006 at 04:26 AM.
    Frans

  2. #2

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: string comparison function

    Something quick I did in five minutes:

    VB Code:
    1. Option Explicit
    2.  
    3. Public Function CompareLongestMatch(String1 As String, String2 As String) As Long
    4.     Dim lngA As Long, lngB As Long, lngC As Long, lngD As Long
    5.     Dim CountTotal As Long
    6.     If LenB(String1) = 0 Then Exit Function
    7.     If LenB(String2) = 0 Then Exit Function
    8.     For lngA = 1 To Len(String1)
    9.         lngB = InStr(String2, Mid$(String1, lngA, 1))
    10.         If lngB > 0 Then
    11.             If CountTotal < 1 Then CountTotal = 1
    12.             lngD = 1
    13.             For lngC = 1 To Len(String1) - lngA
    14.                 If Mid$(String1, lngA + lngC, 1) = Mid$(String2, lngB + lngC, 1) Then
    15.                     lngD = lngD + 1
    16.                 Else
    17.                     Exit For
    18.                 End If
    19.             Next lngC
    20.             If lngD > CountTotal Then CountTotal = lngD
    21.         End If
    22.     Next lngA
    23.     CompareLongestMatch = CountTotal
    24. End Function
    25.  
    26. Private Sub Form_Load()
    27.     MsgBox CompareLongestMatch("123456789012345", "234567890123450")
    28. End Sub


    It has plenty of room for optimization, but I'll leave that to you.

  4. #4

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: string comparison function

    Nicely done.
    I am affraid it is not enough though.
    I forgot to mention that the typo does not need to be at the start or the end.

    eg

    123456789012345
    123456790123450

    should return 14
    Frans

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: string comparison function

    In that case it gets complicated enough not worth of my time to code it completely. But how to do it:
    • make an index of the matches
    • unlike in the code above, also search for the next matches of a character instead of calling InStr only once (this is a bug in my code above which should be fixed)
    • find the longest matches which do not have a collision with eachother; ignore one character long matches


    It shouldn't be too hard after you can get the image of how it works in your mind.

  6. #6

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: string comparison function

    If it was easy, I hadn't asked
    I don't expect anyone to do my work for me, but if someone had a function like that laying around, it would be convenient.

    I guess I have to put some effort in it.

    Thanks for you help anyway.
    Frans

  7. #7
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: string comparison function

    EDIT: Major bugs in this, but its something to work on, its meant to run though string1 until a match isnt found then recursive through again 1 char up the strings.

    i think this works, might be bugs though, only set up for if string1 is the same length as string2.

    VB Code:
    1. Private j  As Long
    2. Private i  As Long, z As Long
    3. Dim string1 As String, string2 As String
    4. Public Sub CompareLongestMatch(ByVal string1 As String, _
    5.                                     ByVal string2 As String)
    6.     If i > Len(string1) Then
    7.     MsgBox z
    8.     Exit Sub
    9.     End If
    10.         Do Until Mid$(string1, i, 1) <> Mid$(string2, j, 1)
    11.             If Mid$(string1, i, 1) = Mid$(string2, j, 1) Then
    12.             If j > Len(string1) Then
    13.             MsgBox z
    14.             Exit Sub
    15.             End If
    16.                ' List1.AddItem z
    17.                 z = z + 1
    18.                 j = j + 1
    19.             End If
    20.             i = i + 1
    21.             DoEvents
    22.               'string1 = "123456789012345"
    23.   '            string2 = "023456789012345"
    24.         Loop
    25.        
    26.         i = i + 1
    27.         j = j + 1
    28.         DoEvents
    29.         CompareLongestMatch string1, string2
    30.    
    31.  
    32. End Sub
    33.  
    34. Private Sub Command1_Click()
    35.  
    36.     i = 1
    37.     j = 1
    38.     string1 = "123456789012345301235777899"
    39.     string2 = "023456789012345401236777799"
    40.     CompareLongestMatch string1, string2
    41.    
    42. End Sub

  8. #8

  9. #9
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: string comparison function

    This should work for any set doesnt matter if strings are different lengths,
    what it does is looks for a match of 2 chars or greater and builds sets.

    VB Code:
    1. Option Explicit
    2. Private j         As Long
    3. Private i         As Long
    4. Private string1   As String
    5. Private string2   As String
    6. Dim z As Long
    7.  
    8. Private Sub Command1_Click()
    9.  
    10.     findagain
    11.  
    12. End Sub
    13.  
    14. Public Sub CompareLongestMatch(ByVal string1 As String, _
    15.                                ByVal string2 As String)
    16.  
    17. Dim strtmp As String
    18.     Do Until Mid$(string1, i, 1) <> Mid$(string2, j, 1)
    19.        
    20.         strtmp = strtmp & Mid$(string1, i, 1)
    21.         j = j + 1
    22.         i = i + 1
    23.    List1.AddItem strtmp
    24.        
    25.         DoEvents
    26.     Loop
    27.   If Len(strtmp) > 1 Then
    28.    List1.AddItem strtmp
    29.    
    30.    z = z + Len(strtmp)
    31.   End If
    32.    
    33. strtmp = ""
    34. End Sub
    35.  
    36. Private Sub findagain()
    37.  
    38.     i = 1
    39.     j = 1
    40.     If Len(string2) = 0 Then
    41.     MsgBox z
    42.         Exit Sub
    43.     End If
    44.     For i = 1 To Len(string1)
    45.         If Mid$(string1, i, 1) = Mid$(string2, j, 1) Then
    46.             CompareLongestMatch string1, string2
    47.             string2 = Mid$(string2, j, Len(string2))
    48.            
    49.             Exit For
    50.         End If
    51.     Next i
    52. findagain
    53. End Sub
    54.  
    55. Private Sub Form_Load()
    56.  
    57.     string1 = "123456789012345" & vbNullChar
    58.     string2 = "123456790123450" & vbNullString
    59.  
    60. End Sub

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: string comparison function

    Give it a try
    VB Code:
    1. Private Function getMatchCount(pStr1 As String, pStr2 As String) As Long
    2. Dim i               As Long, Total As Long
    3. Dim Tmp             As Long
    4. Dim TmpChar         As String
    5. Dim tmpStrTotal     As String
    6. Dim LongestString   As Long
    7.  
    8.     Total = 0
    9.     tmpStrTotal = vbNullString
    10.     LongestString = 0
    11.    
    12.     For i = 1 To Len(pStr2)
    13.         TmpChar = Mid(pStr2, i, 1)
    14.         tmpStrTotal = tmpStrTotal & TmpChar
    15.         Tmp = InStr(1, pStr1, tmpStrTotal, 1)
    16.        
    17.         If CBool(Tmp) Then
    18.             If Len(tmpStrTotal) > LongestString Then
    19.                 Total = Total + 1
    20.                 LongestString = LongestString + 1
    21.             End If
    22.         Else
    23.             If CBool(InStr(1, pStr1, TmpChar, 1)) Then
    24.                 tmpStrTotal = TmpChar
    25.             Else
    26.                 tmpStrTotal = vbNullString
    27.             End If
    28.         End If
    29.     Next
    30.     getMatchCount = Total
    31. End Function
    Last edited by jcis; Jan 11th, 2006 at 04:51 PM.

  11. #11
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: string comparison function

    Frans C: I got some idle time, so I coded it. I put some more thought on it and came up with something relatively simple: no need to keep an index.

    Here is the code:

    VB Code:
    1. Option Explicit
    2.  
    3. Public Function GetMatchString(String1 As String, String2 As String) As String
    4.     Dim lngA As Long, lngB As Long, lngC As Long, lngD As Long
    5.     Dim LongestString As String, CurString As String, CompareString As String
    6.    
    7.     If LenB(String1) = 0 Then Exit Function
    8.     If LenB(String2) = 0 Then Exit Function
    9.    
    10.     For lngA = 1 To Len(String1)
    11.         CompareString = String2
    12.         lngB = 0
    13.         Do Until lngB > 0 Or lngA > Len(String1)
    14.             lngB = InStr(CompareString, Mid$(String1, lngA, 1))
    15.             If lngB < 1 Then lngA = lngA + 1
    16.         Loop
    17.         If lngA > Len(String1) Then Exit For
    18.         Do
    19.             lngC = 1
    20.             Do
    21.                 If lngA + lngC > Len(String1) Then Exit Do
    22.                 If lngB + lngC > Len(CompareString) Then Exit Do
    23.                 If Mid$(String1, lngA + lngC, 1) = Mid$(String2, lngB + lngC, 1) Then
    24.                     If lngC = 1 Then CurString = CurString & Mid$(String1, lngA, 1): Mid$(CompareString, lngB, 1) = vbNullChar
    25.                     CurString = CurString & Mid$(String1, lngA + lngC, 1)
    26.                     Mid$(CompareString, lngB + lngC, 1) = vbNullChar
    27.                     lngC = lngC + 1
    28.                 Else
    29.                     If lngC = 1 Then
    30.                         lngD = InStr(lngB + 1, CompareString, Mid$(String1, lngA, 1))
    31.                         If lngD > 1 Then
    32.                             lngB = lngD
    33.                         Else
    34.                             Exit Do
    35.                         End If
    36.                     Else
    37.                         Exit Do
    38.                     End If
    39.                 End If
    40.             Loop
    41.             lngD = 0
    42.             Do Until lngD > 0 Or lngA + lngC > Len(String1)
    43.                 lngD = InStr(CompareString, Mid$(String1, lngA + lngC, 1))
    44.                 If lngD < 1 Then lngC = lngC + 1
    45.             Loop
    46.             If lngD > 0 Then
    47.                 lngB = lngD
    48.                 lngA = lngA + lngC
    49.             Else
    50.                 Exit Do
    51.             End If
    52.         Loop
    53.         If LenB(CurString) > LenB(LongestString) Then LongestString = CurString
    54.         CurString = vbNullString
    55.     Next lngA
    56.     GetMatchString = LongestString
    57. End Function
    58. Private Sub Form_Load()
    59.     MsgBox GetMatchString("123456789012345", "123456790123450")
    60.     MsgBox GetMatchString("it works nicely!", " nicely, it works!")
    61. End Sub

    As you can see, it returns the matched characters. If you only want to get the number of characters, just use Len()


    Edit: Polished it! It should now work well enough for any combination you need
    Last edited by Merri; Jan 11th, 2006 at 07:04 PM.

  12. #12
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: string comparison function

    Let's GetTickCount all methods

  13. #13
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: string comparison function

    Your function doesn't work as well as mine does, try the samples My priority was making something that works, not something that works fast (and I'm all too tired to even think about that it being 3 AM already).

  14. #14
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: string comparison function

    Quote Originally Posted by jcis
    Let's GetTickCount all methods
    sounds good to me, add mine as well..

  15. #15
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: string comparison function

    just for laughs..

    my pc specs, pretty darn low for this comp, 900mhz cpu, 128 mb ram, 16 mb video card, have a killer rig but is out of order, needs a new hardrive, so anyway for this machine for this string i get a tickcount of 5128,

    1234567890123451234567890123451234567890123451234567890123451234567890123451234567890123451234567890 1234512345678901234512345678901234512345678901234512345678901234512345678901234512345678901234512345 6789012345123456789012345123456789012345123456789012345123456789012345123456789012345123456789012345 1234567890123451234567890123451234567890123451234567890123451234567890123451234567890123451234567890 1234512345678901234512345678901234512345678901234512345678901234512345678901234512345678901234512345 6789012345123456789012345123456789012345123456789012345123456789012345123456789012345123456789012345
    returns 600 btw.

  16. #16
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: string comparison function

    whoops removed the doevents now get 2 for the gettickcount, forget it getting to OT.

  17. #17
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: string comparison function

    Is that one string or two strings? If one, what is the other string you compare to?


    Anyways, my thoughts came up with yet-another way to do this. Since I'm too tired to code it, I'll just describe it:
    • First, check if the whole string matches (of the length of the second comparable string)
    • If not, then take a string that is shorter by one than the comparable string and see if that is found. Move in the search string until a match is found or there is no space
    • If no match is found, shrink again and keep doing it
    • If a match is found, drop these characters and work with what is still left, doing the same as above, until there is really nothing to look at


    I think this would be a pretty much of a flawless solution. My current code isn't able to find all search strings "perfectly", for example:

    VB Code:
    1. MsgBox GetMatchString(_
    2. " this inn is a wonderful inn as I were sleeping I saw dreams ", _
    3. " I saw dreams while sleeping as I were in this wonderful inn ")

    What I might expect it to return would be:

    Code:
     wonderful inn  I saw dreams as I were sleeping this in in
    Meaning, it would return the longest matches first and then go for shorter valid ones.
    Last edited by Merri; Jan 11th, 2006 at 09:33 PM.

  18. #18
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: string comparison function

    Quote Originally Posted by Merri
    Is that one string or two strings? If one, what is the other string you compare to?


    Anyways, my thoughts came up with yet-another way to do this. Since I'm too tired to code it, I'll just describe it:
    • First, check if the whole string matches (of the length of the second comparable string)
    • If not, then take a string that is shorter by one than the comparable string and see if that is found. Move in the search string until a match is found or there is no space
    • If no match is found, shrink again and keep doing it
    • If a match is found, drop these characters and work with what is still left, doing the same as above, until there is really nothing to look at


    I think this would be a pretty much of a flawless solution. My current code isn't able to find all search strings "perfectly", for example:

    VB Code:
    1. MsgBox GetMatchString(_
    2. " this inn is a wonderful inn as I were sleeping I saw dreams ", _
    3. " I saw dreams while sleeping as I were in this wonderful inn ")

    What I might expect it to return would be:

    Code:
     wonderful inn  I saw dreams as I were sleeping this in in
    Meaning, it would return the longest matches first and then go for shorter valid ones.
    just skimmed through what you wrote but i think thats what i did, i started with string1 and ran though it 1 char at a time comparing each of these to the first char in string2 until i got a match if a match was found then see how long the match goes on for then remove that from string2 and repeat this until string2 = 0 if no match is found then run though string1 again this time starting at the next char in string2.

  19. #19
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: string comparison function

    No, that's not it, its the opposite: first take as much of string1 as possible (= the length of string2). Compare it all to string2. If no match and if there are more chars in string1, move to right by one, take a string, compare to string2 and so on. Then when this is done and no matches, shrink the lookup string by one, check through string1 and this time as the lookup string is shorter than string2, you need to check more from string2 as well. Then keep doing this and mark out matches until there is nothing more to look for (= have only one character long matches which we ignore).

    Marking out always the longest first will get rid of the problem that is with the example strings I posted in my last message.

  20. #20

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: string comparison function

    Thank you guys.
    I will use Merri's method, because it is the only one that works correctly.
    jcis's method doen't always return the correct results.
    Jmacp's method produces an out of stack space error with the following strings:
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     string1 = "123456790123450" & vbNullChar
    4.     string2 = "123456789012345" & vbNullString
    5.  
    6. End Sub
    Although it returns the correct result when the strings are switched.

    Case closed.
    Frans

  21. #21
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: string comparison function

    Quote Originally Posted by Frans C
    Thank you guys.
    I will use Merri's method, because it is the only one that works correctly.
    jcis's method doen't always return the correct results.
    Jmacp's method produces an out of stack space error with the following
    Case closed.
    Yes, I didn't read your post #4, sorry.
    And yes, Merri's method is working fine, except for this cases:

    123456789012345
    023456789012345

    and

    123456789012345
    234567890123450
    (both examples in your first post)
    Shouldn't be 14? here is returning 15 for both of them.
    (Maybe I should substract 1 from the result, i don't know)
    Last edited by jcis; Jan 12th, 2006 at 03:26 AM.

  22. #22

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: [RESOLVED] string comparison function

    Yes, you are right.
    I guess it isn't resolved after all.
    Frans

  23. #23

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: string comparison function

    I picked some ideas from all of you, and wrote the following function.
    I tested it with all examples, and it looks like it works.

    I took another thing into consideration.
    AAABB
    BBAAA
    should not return 5 but 3. I don't want to accept swapping characters as if the strings would be identical.

    Again, thank all of you for the effort you have put into it.

    VB Code:
    1. Private Sub Command1_Click()
    2. MsgBox GetLongestMatch("123056789012345", "123456789012345")
    3. End Sub
    4.  
    5. Private Function GetLongestMatch(ByVal String1 As String, ByVal String2 As String) As Long
    6. Dim strHelp As String
    7. Dim lngA As Long
    8. Dim lngB As Long
    9. Dim blnStart As Boolean
    10. Dim strSearch As String
    11. Dim lngMatch As Long
    12. Dim strRemaining As String
    13.     If String1 = String2 Then
    14.         lngMatch = Len(String1)
    15.     Else
    16.         If Len(String1) > Len(String2) Then
    17.             ' we want string1 to be the shortest string
    18.             strHelp = String2
    19.             String2 = String1
    20.             String1 = strHelp
    21.         End If
    22.         For lngA = Len(String1) To 1 Step -1
    23.             ' try to find the left characters of string1 in string2
    24.             strSearch = Left$(String1, lngA)
    25.             If InStr(String2, strSearch) > 0 Then
    26.                 lngMatch = Len(strSearch)
    27.                 Exit For
    28.             Else
    29.                 ' try to find the right characters of string1 in string2
    30.                 strSearch = Right$(String1, lngA)
    31.                 If InStr(String2, strSearch) > 0 Then
    32.                     lngMatch = Len(strSearch)
    33.                     Exit For
    34.                 End If
    35.             End If
    36.         Next
    37.         If lngMatch > 0 Then
    38.             ' check for longest match in remaining strings (before and after matching strings)
    39.             lngA = InStr(String1, strSearch)
    40.             lngB = InStr(String2, strSearch)
    41.             If lngA > 1 And lngB > 1 Then
    42.                 ' there are strings before the match, check these
    43.                 lngMatch = lngMatch + GetLongestMatch(Left$(String1, lngA - 1), Left$(String2, lngB - 1))
    44.             End If
    45.             If (lngA + Len(strSearch) < Len(String1)) And (lngB + Len(strSearch) < Len(String2)) Then
    46.                 ' there are strings after the match, check these
    47.                 lngMatch = lngMatch + GetLongestMatch(Right$(String1, Len(String1) - lngA - Len(strSearch) + 1), Right$(String2, Len(String2) - lngB - Len(strSearch) + 1))
    48.             End If
    49.         End If
    50.     End If
    51.     GetLongestMatch = lngMatch
    52. End Function
    Frans

  24. #24

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: [RESOLVED] string comparison function

    Sorry, it still had an error it the logic.
    This should work though.

    VB Code:
    1. Private Function GetLongestMatch(ByVal String1 As String, ByVal String2 As String) As Long
    2. Dim strHelp As String
    3. Dim lngA As Long
    4. Dim lngB As Long
    5. Dim blnStart As Boolean
    6. Dim strSearch As String
    7. Dim lngMatch As Long
    8. Dim strRemaining As String
    9. Dim blnFound As Boolean
    10.     If String1 = String2 Then
    11.         lngMatch = Len(String1)
    12.     Else
    13.         If Len(String1) > Len(String2) Then
    14.             ' we want string1 to be the shortest string
    15.             strHelp = String2
    16.             String2 = String1
    17.             String1 = strHelp
    18.         End If
    19.         For lngA = Len(String1) To 1 Step -1
    20.             ' try to find characters of string1 in string2
    21.             ' we start with the longest possible string, and if not found, make the string smaller
    22.             For lngB = 1 To Len(String2) - lngA + 1
    23.                 ' roam through string1 from left to right
    24.                 strSearch = Mid$(String1, lngB, lngA)
    25.                 If InStr(String2, strSearch) > 0 Then
    26.                     lngMatch = Len(strSearch)
    27.                     blnFound = True
    28.                     Exit For
    29.                 End If
    30.             Next
    31.             If blnFound Then Exit For
    32.         Next
    33.         If lngMatch > 0 Then
    34.             ' check for longest match in remaining strings (before and after matching strings)
    35.             lngA = InStr(String1, strSearch)
    36.             lngB = InStr(String2, strSearch)
    37.             If lngA > 1 And lngB > 1 Then
    38.                 ' there are strings before the match, check these
    39.                 lngMatch = lngMatch + GetLongestMatch(Left$(String1, lngA - 1), Left$(String2, lngB - 1))
    40.             End If
    41.             If (lngA + Len(strSearch) < Len(String1)) And (lngB + Len(strSearch) < Len(String2)) Then
    42.                 ' there are strings after the match, check these
    43.                 lngMatch = lngMatch + GetLongestMatch(Right$(String1, Len(String1) - lngA - Len(strSearch) + 1), Right$(String2, Len(String2) - lngB - Len(strSearch) + 1))
    44.             End If
    45.         End If
    46.     End If
    47.     GetLongestMatch = lngMatch
    48. End Function
    Frans

  25. #25
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: [RESOLVED] string comparison function

    i think i fixed my version,

    VB Code:
    1. Option Explicit
    2. Private j         As Long
    3. Private i         As Long
    4. Private string1   As String
    5. Private string2   As String
    6. Private z         As Long
    7. Private blIsAMatchFoundBetweenStrings    As Boolean
    8.  
    9. Private Sub Command1_Click()
    10.  
    11.     string1 = "123456790123450"
    12.     string2 = "123456789012345"
    13.     blIsAMatchFoundBetweenStrings = False
    14.     IterateString
    15.  
    16. End Sub
    17.  
    18. '***
    19. Public Sub CompareLongestMatch(ByVal string1 As String, _
    20.                                ByVal string2 As String)
    21.  
    22. Dim strTmpString As String
    23.  
    24.     ' when a match is found this sub starts, the value of i is carried on
    25.     ' from the previous IterateString sub
    26.    
    27.     ' string1 = "123456790123450"
    28.     ' string2 = "123456789012345"
    29.     ' so if i = 1 run through this till i <> j
    30.     ' first run up till 8, finds, 1234567
    31.    
    32.    
    33.     Do Until Mid$(string1, i, 1) <> Mid$(string2, j, 1)
    34.         strTmpString = strTmpString & Mid$(string1, i, 1)
    35.         j = j + 1
    36.         i = i + 1
    37.         DoEvents
    38.     Loop
    39.    
    40.     ' the length of all Consecutive  letters found
    41.     If Len(strTmpString) > 1 Then
    42.         z = z + Len(strTmpString)
    43.     End If
    44.    
    45.    
    46.     strTmpString = vbNullString
    47.  
    48. End Sub
    49.  
    50. Private Sub IterateString()
    51.  
    52.     i = 1
    53.     j = 1
    54.    
    55.     ' when the length of string2 = 0, end, will always reach 0
    56.     If Len(string2) = 0 Then
    57.         MsgBox z
    58.         z = 0
    59.         Exit Sub
    60.     End If
    61.    
    62.     'string1 = "123456790123450"
    63.     'string2 = "123456789012345"
    64.    
    65.     For i = 1 To Len(string1)
    66.     ' run through string1 1 char at a time and compare each to first char in string2
    67.     ' j stays at 1 for first loop
    68.    
    69.         If Mid$(string1, i, 1) = Mid$(string2, j, 1) Then
    70.         'if a match is found then find out how long it goes on for   ***
    71.             CompareLongestMatch string1, string2
    72.            
    73.             ' redim string2 so its string2 -
    74.             ' so for the above strings 1234567 are found so string2 becomes
    75.             ' 89012345, string1 doesn't change
    76.             ' 8 isnt found so string2 =9012345 then
    77.             ' 9012345 so strin2 = 0
    78.            
    79.            
    80.             string2 = Mid$(string2, j, Len(string2))
    81.             DoEvents
    82.             blIsAMatchFoundBetweenStrings = False
    83.             Exit For
    84.         End If
    85.     Next i
    86.    
    87.     'if no match is found then that character must be a single char then remove it
    88.     ' so this next condition only gets entered if no match was found
    89.    
    90.     If blIsAMatchFoundBetweenStrings Then
    91.     ' so redim string , string2 - 1
    92.         string2 = Right$(string2, Len(string2) - 1)
    93.     End If
    94.    
    95.     ' set the flag back to true
    96.     ' when a match is found flag sets to false
    97.     blIsAMatchFoundBetweenStrings = True
    98.    
    99.     ' recursive
    100.     IterateString
    101.  
    102. End Sub

  26. #26

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: [RESOLVED] string comparison function

    Nice, but try this:
    string1 = "1234500000"
    string2 = "1234512345"
    It should return 5
    Frans

  27. #27
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: [RESOLVED] string comparison function

    Quote Originally Posted by Frans C
    Nice, but try this:
    string1 = "1234500000"
    string2 = "1234512345"
    It should return 5

    pretty major typo , but i see whats happening,

    string2 starts at 1234512345 then reduces to 12345 so a second match is found, will leave at the moment...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width