Solved

cross check words in string

Posted on 2011-03-10
13
329 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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 …

738 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