Show differences in two strings?

# Thread: Show differences in two strings?

1. elaine pack Guest

## Show differences in two strings?

I have a "before and after" string of text captured from two different db tables in my .asp, and if they are different, my user wishes to see WHAT the differences are. Is there a relatively easy way to do this?<BR><BR>BEFORE<BR>The quick brown fox jumped over the lazy black dog.<BR><BR>AFTER<BR>The quick brown bear jumped over the lazy brown dog.<BR><BR>DIFFERENCES <BR>fox--&#062;bear<BR>black--&#062;brown<BR><BR>Thanks!<BR><BR>elaine<BR>epack@ blue.unix.virginia.edu

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## *CAN* be very, very tough!

Suppose the two strings are:<BR><BR>The quick brown fox jumped over the lazy dogs.<BR>Quick black bears often jump over dogs and people.<BR><BR>What is the definition of "differences" now???<BR><BR>If *you* can describe how to tell what a "difference" is, in clear and concise English, then we can try to write the code. But as it is???? The question is too general, I think.<BR><BR>

3. Medieval Dude Guest

## RE: *CAN* be very, very tough!

I think what complicates it even more is the fact that we have words within each string. Does that mean splitting the string apart into words and then comparing? For instance:<BR>The quick brown fox jumped over the lazy dogs.<BR>The quick blue fox jumped over the lazy dogs.<BR>If there is no breaking of words, the difference is:<BR>lue fox jumped over the lazy dogs-&#062;rown fox jumped over the lazy dogs<BR>

4. Senior Member
Join Date
Dec 1969
Posts
614

## Wacky Suggestion?

MS_WORD has a file compare function- could it possible to use this via VBA? - (sounds too stupid to be practical - I cann&#039;t believe that I&#039;ve written it now)...<BR><BR>Anyway,I ran the following test files.....<BR><BR>File1: The quick brown fox jumped over the lazy cat<BR>File2: The quick fox jumped over the lazy and stupid cat<BR><BR>Result:It hightlighted both the &#039;BROWN&#039; & "AND STUPID" changes - so,the algorithm must have a way of re-syncing after differences were found.<BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not so wacky...

Depending on how often this function is needed, "scripting" MS Word to do it might, indeed, work fine.<BR><BR>Or maybe find a "diff" function a la Unix and invoke it from the command line.<BR><BR>Neither of these are a good idea if this is something that will be done thousands of times an hour (becuz of the overhead of getting to MS Word or an executable), but if it&#039;s only a dozen or so (maybe even a hundred or so) per hour... Well, why not?<BR><BR>Why reinvent the wheel.

6. Senior Member
Join Date
Dec 1969
Posts
1,505

## what exactly is the question?

is it that one string is in one table and the other one is in different table?

7. elaine pack Guest

## RE: what exactly is the question?

&#062;is it that one string is in one table and the other one is in different table?<BR><BR>Yes. The application in question is a job description db, and multiple users can submit a change to any job description in their departments. My user, who is in HR, says that there could be just a single word changed, for example, and that could be difficult for her to detect. Since the changes go to a "holding" table for review by HR (HR posts it to the permanent table) I have both the "old" and "new" fields available for comparison. <BR><BR>If this isn&#039;t easily done, it&#039;s no big deal, but it would make our HR folks&#039; lives much easier.<BR><BR>Thanks!<BR>elaine<BR>epack@blue.uni x.virginia.edu

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Well, one stab at it...

If you&#039;d be willing to simply see the *first* point of difference, you could do this:<BR><BR>&#060;%<BR>words1 = Split( string1, " " ) &#039; separate first string into words<BR>words2 = Split( string2, " " ) &#039; ditto second string<BR>size1 = UBound( words1 )<BR>size2 = UBound( words2 ) &#039; count words in each<BR>If size1 &#060; size2 Then minsize = size1 Else minsize = size2<BR><BR>For w = 0 To minsize<BR>&nbsp; &nbsp; If words1(w) &#060;&#062; words2(w) Then<BR>&nbsp; &nbsp; &nbsp; &nbsp; &#039; found a difference! report it:<BR>&nbsp; &nbsp; &nbsp; &nbsp; words1(w) = "&#060;B&#062;" & words1(w) & "&#060;/b&#062;"<BR>&nbsp; &nbsp; &nbsp; &nbsp; words2(w) = "&#060;B&#062;" & words2(w) & "&#060;/b&#062;"<BR>&nbsp; &nbsp; &nbsp; &nbsp; Response.Write Join(words1) & "&lt;BR&#062;"<BR>&nbsp; &nbsp; &nbsp; &nbsp; Response.Write Join(words2) & "&lt;BR&#062;"<BR>&nbsp; &nbsp; &nbsp; &nbsp; Exit For<BR>&nbsp; &nbsp; End If<BR>Next<BR>%&#062;<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
614

## ;-)

&#060;&#060;but it would make our HR folks&#039; lives much easier. &#062;&#062;<BR>and that would be a good idea because.....?<BR><BR>;-) <BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
1,505

## RE: ;-)

going ahead with what Bill said, find out the length of both the strings and then whichever is bigger, can&#039;t we just use "MINUS" to achieve the result.<BR>select longer_string minus shorter_string from table.

#### Posting Permissions

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