Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Showing the difference between two strings

    I have two variables each holding a string of data. The string may be a simple short sentence of text, it may be several paragraphs of text.

    Call one "Original", and the other "Modified"
    The "Modified" string is a modified version of the the "Original".

    On my ASP page I need to show both strings side by side, that's no problem! However to aid my users in finding the modifications I want to highlight using a background color or font color where the modified string differs from the original.

    I was just wondering if anyone had tackled anything like this before?

    I would really appreciate any help you could give me.

    Mary Hill.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whell, In VB you have a stringfunction that's Mid(string,geginning,number) and it does the following

    example : Mid(abcdef,4,2) returns the string "de"

    if you would make something like

    dim string1, string2

    string1=request.form("string1")
    string2=request.form("string2")

    if Mid(string1,1,1) <> Mid(string2,1,1) then
    response.write("<font color='red'> & Mid(string1,1,1) & "</font>")
    else
    response.write(Mid(string1,1,1))
    end if


    you would see if the first caracter of the two strings is equal (black font) or not (red font).

    You could do this for every possition in the strings. I cant immediatly come up with some kind of loop-function to do it in an ellegant way, buth perhaps someone else can.

    (One way is to break up the string on your form and place every caracter in one textfield and then use a
    for each input in request.form
    (above code)
    next
    loop. buth it's not user-friendly)

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm... just brainstorming here... but why not split up both strings by the SPACE character (" ") into two arrays, and then just try to match the words?

    Once a word doesn't match, perhaps run a second nested loop that highlights the words until they match the current word, and then break out of the nested loop and continue matching the arrays?
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice thinking. Buth what happens if a user just inserts one space to many in one of the strings ?
    If you get an empty cell in your array, all the following words won't match, and it might not be very clear for the user what the difference was.
    I don't think their is an easy sollution here. You'll probaby have to run more then one routine here. (perhaps depending on the length of the string)

    something like

    if Len(string1) < 20 then
    routine1
    else
    if Len(string1) < 50 then
    routine2
    ...
    Last edited by raf; 08-08-2002 at 08:48 AM.

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    I think i got it sorta going where whammy suggested but there is some recursive error.

    Code:
    <%
    Dim String1, String2
    String1 = Request.Form("String1")
    String2 = Request.Form("String2")
    
    If String1 <> "" And String2 <> "" Then
    Dim Words1(800)
    Dim Words2(800)
    Dim x, y, temp
    x = 1
    y = 1
        
        'Seperate the first sentence into a string
        Do
        temp = InStr(x, String1, " ", vbTextCompare)
        If temp = 0 Then Exit Do
        Words1(y) = Mid(String1, x, temp - 1)
        x = temp + 1
        y = y + 1
        Loop Until x = Len(String1)
        
        temp = ""
        For z = 1 To y
        temp = temp & Words1(z) & vbCrLf
        Next z
        
        
    End If
    %>
    <p><FORM action="" Method="POST">
    <INPUT type="text" name="String1">
    <BR><BR>
    <INPUT type="text" name="String2">
    <BR>
    <INPUT type="Submit" value="Compare">
    </FORM>
    </p>
    <p>
    <%=temp%>
    </p>
    Perhaps someone can see whats wrong with it. Try the code out and you'll see the problem.
    OracleGuy

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Mary Hill


    Are both the original and modified strings going to be pre written with no user inter-action?


  •  

    Posting Permissions

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