Solved

cross check words in string

Posted on 2011-03-10
13
306 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
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
 

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

863 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

19 Experts available now in Live!

Get 1:1 Help Now