Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cross check words in string

Posted on 2011-03-10
13
Medium Priority
?
342 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

661 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