Solved

cross check words in string

Posted on 2011-03-10
13
291 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:PeterBaileyUk
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 5

Expert Comment

by:mayankagarwal
Comment Utility
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.
0
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
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?
0
 
LVL 5

Expert Comment

by:mayankagarwal
Comment Utility
one more option is, split both the strings from spaces. and then compare the splitted strings
0
 

Author Comment

by:PeterBaileyUk
Comment Utility
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"

0
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
Comment Utility
This function looks for all the words in str1 to be in str2 and returns true or false.

usage :
if matchstrings(str1, str2) = true then...

to perform the match both ways you could do..
if matchstrings(str1,str2) = true and matchstrings(str2,str1) = true then...

Note that it assumes words are separated by one space only.


Function MatchStrings(str1 As String, str2 As String)

' assumes words are separated by one space only.
Dim astr1
Dim astr2
Dim ub1, ub2
Dim x As Integer, y As Integer
Dim matchflag As Boolean
MatchStrings = True ' assume a match
If str1 = str2 Then Exit Function ' strings are the same

astr1 = Split(str1, " ")
astr2 = Split(str2, " ")
ub1 = UBound(astr1)
ub2 = UBound(astr2)
If ub1 > ub2 Then   ' str1 has more than str2
    MatchStrings = False
    Exit Function
End If

For x = 0 To ub1
    matchflag = False
    For y = 0 To ub2
        If astr1(x) = astr2(y) Then
            astr2(y) = "is matched"  'prevent two matches to same value
            matchflag = True ' record match for test below
            Exit For  ' don't test amy further
        Else
            'not matched yet
        End If
    Next y
    If matchflag = False Then
        MatchStrings = False   ' any false ends the comparison
        Exit Function
    End If
Next x

End Function


0
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
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

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 29

Expert Comment

by:Badotz
Comment Utility
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

0
 

Author Closing Comment

by:PeterBaileyUk
Comment Utility
thank you
0
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
(sigh) Nothing for elegance, eh?

No worries...
0
 

Author Comment

by:PeterBaileyUk
Comment Utility
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.

:)
0
 
LVL 29

Expert Comment

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

As I said, no worries...
0
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
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.
0
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now