Solved

cross check words in string

Posted on 2011-03-10
13
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35093998
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
ID: 35094003
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
ID: 35094015
one more option is, split both the strings from spaces. and then compare the splitted strings
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PeterBaileyUk
ID: 35094033
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
ID: 35094193
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
ID: 35094855
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
 
LVL 29

Expert Comment

by:Badotz
ID: 35094874
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
ID: 35095579
thank you
0
 
LVL 29

Expert Comment

by:Badotz
ID: 35095643
(sigh) Nothing for elegance, eh?

No worries...
0
 

Author Comment

by:PeterBaileyUk
ID: 35095956
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
ID: 35096144
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
ID: 35096293
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
ID: 35096490
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
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.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

749 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