Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

cross check words in string

I have two strings one from last months data set and one which is this months data set.

The word order has been changed so
str1 =MULTIJET C POP
str2=C POP MULTIJET

str1=C LOUNGE MULTIJET
str2=MULTIJET C LOUNGE

str1=DYNAMIC T-JET
str2=T-JET DYNAMIC

what I would like to do is see if each complete word is in the str2 and if it is then return true else false.

this way I could ignore strings that fit this criteria.

I am in access vba
Avatar of mayankagarwal
mayankagarwal
Flag of India image

try one thing if you can take this string in an array and sort the array character by character then do the equal to operator.
Avatar of peter57r
Need to be absolutely precise about what you want here.

... see if each complete word is in the str2 ...

So that means the test is just one-way?  If all words in str1 are in str2 then you are regarding that as a match even if there are extra words in str2 that don't appear in str1?
one more option is, split both the strings from spaces. and then compare the splitted strings
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

effectively the strings with the same words in (not partial match) albeit the other way around are the same so dont need further action.

I assume 1: they will be equal length (although an additional space could be a problem)
The words contained in the strings will match word for word.

extra words indicate further action.

so "peter likes toy story 3"
is same as "likes peter toy story 3"
as is "toy story 3 likes peter"

ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I took a different approach:


Dim source
Dim target

source = "peter likes toy story 3"
target = "toy story 3 likes peter"

result = is_equivalent(source, target)

If result = "" Then
    '' We have a match !
Else
    '' We don't have a match :-(
End If
''
'' ========================================

Function is_equivalent(source, target)

ra = Split(source)

For i = 1 to UBound(ra)
    target = Replace(target, ra(i), "") '' Replace a matching word with ""
next

is_equivalent = Trim(target)

End Function
''
'' ========================================

Open in new window

Oops, forgot to declare:


Function is_equivalent(source, target)

dim ra
dim i

ra = Split(source)

For i = 1 to UBound(ra)
    target = Replace(target, ra(i), "") '' Replace a matching word with ""
next

is_equivalent = Trim(target)

End Function
''
'' ========================================

Open in new window

thank you
(sigh) Nothing for elegance, eh?

No worries...
alas Badotz on this occasion no, i tried both solutions BUT Peters warranted full points and he will lay testamant that on most occasions I do split points where multiple solutions exist but the solution he posted was more thorough.

Keep an eye out as I post a lot and no reason points cannot be given later.

:)
If something doesn't work, a note to that effect is always helpful ;-)

As I said, no worries...
I nearly went that route Badotz, but realised that replace would replace strings within words as well as complete words, and I couldn't see any easy way round that.
True, although it is not obvious from the examples that that would be a problem.

Ordering by word length and processing the longest first would help, but as you say, would also add complexity.