Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

Compare two strings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frankbelly - 10 Nov 2006 01:16 GMT
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.
Jezebel - 10 Nov 2006 02:16 GMT
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.

>I would like to compare two strings and highlight only the differences in
>the
[quoted text clipped - 17 lines]
>
> Any help is greatly appreciated.
Frankbelly - 10 Nov 2006 02:37 GMT
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.

> You need to define 'difference' more fully. What would you highlight here --
>
[quoted text clipped - 26 lines]
> >
> > Any help is greatly appreciated.
Jezebel - 10 Nov 2006 03:23 GMT
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.

> Yes, it would be character by character.
>
[quoted text clipped - 53 lines]
>> >
>> > Any help is greatly appreciated.
Frankbelly - 10 Nov 2006 03:39 GMT
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

> Sorry, you've misunderstood a little. If you do character-by-character
> comparison, then using
[quoted text clipped - 65 lines]
> >> >
> >> > Any help is greatly appreciated.
Jay Freedman - 10 Nov 2006 03:43 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Sorry, you've misunderstood a little. If you do character-by-character
>comparison, then using
[quoted text clipped - 65 lines]
>>> >
>>> > Any help is greatly appreciated.
Frankbelly - 10 Nov 2006 04:04 GMT
Great!

thank you both so much (Jezebel and Jay)

I really appreciate your help.

> And that's only the case where characters have been added to the
> original string. Then you need to consider cases where characters have
[quoted text clipped - 89 lines]
> >>> >
> >>> > Any help is greatly appreciated.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.