Compare two strings

F

Frankbelly

I would like to compare two strings and highlight only the differences in the
second string.

Example:

Compare The Dog is out
with The Dog i's out

Results The Dog i's out <--The appostraphe would be in red font color

I am performing this comparison in a textbox located in a userform, so I
wonder if there is a way to highlight only the difference (the appostraphe in
the above example) and have this phrase displayed in a textbox on the
userform with the difference highlighted.

I know, crazy request isn't it?

Any help is greatly appreciated.
 
J

Jezebel

You need to define 'difference' more fully. What would you highlight here --
Compare The Dog is out
with The Dog iXXs out

'XX' or 'XXs out' ? .... You can see how the logic could get *very*
complicated, depending on whether you're doing straight
character-by-character or partial string matching.
 
F

Frankbelly

Yes, it would be character by character.

Compare The Dog is out
with The Dog iXXs out

the entire phrase (The Dog iXXs out) wout appear in the textbox with both
XX's highlighted in, preferably, red text or some other method of making
those characters stand out. It would be a character by character comparison.
Each character that is different in the second phrase would be highlighted.

Compare The Dog is out
with The DXog is oXuXt

The phrase would appear in the textbox as

The DXog is oXuXt

with all of the X's highlighted in red making it very clear to the user that
these characters are what is different from the first phrase.
 
J

Jezebel

Sorry, you've misunderstood a little. If you do character-by-character
comparison, then using
Compare The Dog is out
with The Dog iXXs out

*all* the characters from the first X onwards are different (not just the
Xs). Character-by-character means comparing the characters in position n in
the two strings: OK if the same, highlight if not. If you don't want 's out'
highlighted, you need extra logic to determine that positions 10-14 in
string 1 match positions 11-15 in string 2.
 
F

Frankbelly

Ah, yes I see. Thank you. You have definitely put me in the right
direction. I will begin experimenting with code which determines how not to
highlight the other letters that match regardless of their position.

I did have one other related question though...Can you have different
colored text within one TextBox on a userform. i.e., the X's in the phrase
would be red while the remaining text is black in the same textbox?

I am doing a comparison between the values of two textboxes on a userform

compare textbox.value1
with textbox.value2
 
J

Jay Freedman

And that's only the case where characters have been added to the
original string. Then you need to consider cases where characters have
been removed, or where some have been added and others have been
removed at the same time. And how about transpositions, either of
adjacent characters or of ones separated by one or more characters?

Once you get a decent definition of the rules, then you'll find that
you can't change the font colors for only some of the text in a
regular text box of a userform -- there's only one ForeColor property
for the entire text box. To do what you want, you'll need a Rich Text
control. There isn't one in the set of controls that come with Office,
so you'll have to find one somewhere else. Unfortunately, you can't
use the one that came with VB 6.0, because it's a security risk and is
therefore disabled (http://support.microsoft.com/?kbid=838010). You
can try this one:
http://www.mvps.org/emorcillo/download/vb6/ctl_riched.msi

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top