Results 1 to 27 of 27

Thread: [RESOLVED] string comparison function

Threaded View

  1. #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.

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