Results 1 to 11 of 11

Thread: Compare two strings and get the differance

  1. #1

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Question Compare two strings and get the differance

    Hi,

    I have 2 strings

    string1 = "abcdefgh"
    string2 = "abcdefghijklm"

    I want to get the difference of the two strings .....

    like getDiff(str2,str1) = "ijklm"

    and where the diffrance is

    like

    getDiffStart(str2,str1) = 8 (where the last commom charcater is)

    ...........

    Please help
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    use the mid$ function in a loop to grab one char at a time from each string and compare them

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you mean something like this? :
    VB Code:
    1. [color=blue]Private Sub[/color] Command1_Click()
    2. MsgBox GetDifference("abcdefgh", "abcdefghijklm")
    3. [color=blue]End Sub[/color]
    4.  
    5. [color=blue]Private Function[/color] GetDifference([color=blue]ByVal[/color] stringA [color=blue]As String[/color], [color=blue]ByVal[/color] stringB [color=blue]As String[/color]) [color=blue]As String[/color]
    6. GetDifference = [color=blue]Replace[/color](stringB, stringA, "")
    7. [color=blue]End Function[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    VB Code:
    1. Private Sub Form_Load()
    2. Debug.Print GetDiff("Hello", "Hellohihow")
    3. Debug.Print getDiffStart("Hello", "Hellohihow")
    4.  
    5. End Sub
    6. Public Function GetDiff(S1 As String, S2 As String) As String
    7.     If Len(S1) > Len(S2) Then
    8.         GetDiff = Replace(S1, S2, "")
    9.     Else
    10.         GetDiff = Replace(S2, S1, "")
    11.     End If
    12. End Function
    13. Public Function getDiffStart(S1 As String, S2 As String) As Integer
    14. Dim Res As String
    15. Res = GetDiff(S1, S2)
    16.   If Len(Res) Then
    17.        If Len(S1) > Len(S2) Then
    18.            getDiffStart = InStr(1, S1, Res)
    19.         Else
    20.             getDiffStart = InStr(1, S2, Res)
    21.        End If
    22.   End If
    23. End Function

  5. #5

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    how abt .........

    What if the two strings are

    s1 = "abcdefgh"

    s2 = "abgh"

    I need to get the difference which is "cde" and where it starts .....

    I have richtext box called rtf1

    and when the user presses my "Undo" or "Redo" button I want to highlite the difference .....

    like in worpad when you delete something and press CTRL + Z the part you delected get's highlited ........


    I currently use something like this .........

    Code:
    ' The Redo Part .......... Very Messy Coding
    
    'On Error Resume Next
    ignore = True
    LockWindowUpdate Me.hwnd
    If Not undo(undocount + 1) = "" Then
        SelStart = rtfText.SelStart
        sellen = rtfText.SelLength
       
        undocount = undocount + 1
        RichText1.TextRTF = undo(undocount)
        
        If Len(RichText1.Text) > Len(rtfText.Text) Then
            If Left(RichText1.Text, Len(rtfText.Text)) = rtfText.Text Then
                SelStart = Len(rtfText.Text)
                sellen1 = Abs(Len(RichText1.Text) - Len(rtfText.Text))
            End If
        End If
        
        rtfText.TextRTF = undo(undocount) ' load the next text
        Timer5.Enabled = False
        
        If Not sellen1 > 0 Then
            rtfText.SelStart = SelStart + sellen
        Else
            rtfText.SelStart = SelStart ' + sellen
            rtfText.SelLength = sellen1
        End If
        
        RichText1.TextRTF = ""
        undolen = Len(rtfText.TextRTF)
        Call rtfText_SelChange
    End If
    LockWindowUpdate 0
    ignore = False
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  6. #6
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    What if the two strings are

    s1 = "abcdefgh"

    s2 = "abgh"

    I need to get the difference which is "cde" and where it starts .....
    use the mid$ function in a loop to grab one char at a time from each string and compare them

  7. #7

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Too complicated

    Originally posted by phinds
    use the mid$ function in a loop to grab one char at a time from each string and compare them
    Well ........ it does not work correctly in strings which contain vbcrlf ..... (I tried)

    The Replace and Instr Combinations work on spesific string stypes i encountered .....

    for eg. s1 = hello'hi'hello'hi'hello'hi'hello'hi'
    s2 = hello'hi'hello''hello'hi'hello'hi'

    U c.......... what i mean ......... when middle 'hi' is deleted and we use the

    instr function to get the start of the difference .......
    Or use replace here ......... it won't work

    ........... Please help if you guys nkow anything I could use to highlight the diffrence in RTFTEXT box when the user deletes some text and presses Ctrl + Z
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  8. #8
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    Well ........ it does not work correctly in strings which contain vbcrlf ..... (I tried)
    don't know what you tried, but the mid$ function doesn't care about vbCrLf and this technique WILL work on any pair of strings. If you have code that "doesn't work", post it.

  9. #9

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Try this

    Originally posted by phinds
    don't know what you tried, but the mid$ function doesn't care about vbCrLf and this technique WILL work on any pair of strings. If you have code that "doesn't work", post it.
    Code:
    Function getdiffstart(str1 as string,str2 as string) as integer
    
    for i = 0 to len(str2) - 1
    
    if mid(str2,i,1) <> mid(str1,i,1) then
       getdiffstart = i
       exit for
    end if
    
    next i
    
    End function
    
    ' the way to get the actual difference is more complex
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  10. #10
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    Can you uh... perhaps shorten your sig a bit? You got to admit, it is blooming huge!

  11. #11

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Hi

    Originally posted by LITHIA
    "Can you uh... perhaps shorten your sig a bit? You got to admit, it is blooming huge!"
    Said the desert to the grain of sand ................ lol
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

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