View Full Version : Showing the difference between two strings
08-05-2002, 02:28 PM
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.
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
if Mid(string1,1,1) <> Mid(string2,1,1) then
response.write("<font color='red'> & Mid(string1,1,1) & "</font>")
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
loop. buth it's not user-friendly)
08-08-2002, 12:51 AM
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?
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)
if Len(string1) < 20 then
if Len(string1) < 50 then
08-09-2002, 01:18 AM
I think i got it sorta going where whammy suggested but there is some recursive error.
Dim String1, String2
String1 = Request.Form("String1")
String2 = Request.Form("String2")
If String1 <> "" And String2 <> "" Then
Dim x, y, temp
x = 1
y = 1
'Seperate the first sentence into a string
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
<p><FORM action="" Method="POST">
<INPUT type="text" name="String1">
<INPUT type="text" name="String2">
<INPUT type="Submit" value="Compare">
Perhaps someone can see whats wrong with it. Try the code out and you'll see the problem.
Are both the original and modified strings going to be pre written with no user inter-action?
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.