Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for a word in a string 1

Status
Not open for further replies.

gkratnam

Programmer
Aug 9, 2007
37
US
Hi,
I am searching for a word in a string using '$', it did not work for the below scenario.

Code:
a = "PETER~SMITH~DAVIDLU"
b = "DAVIDLU"
?ALLTRIM(b)$ a &&This returns TRUE

I was expecting a FALSE. Is there any other way to do this?

Please assist. Thanks!
 
Why should it return false? The $ operator means "is contained in", and DAVIDLU is contained in PETER~SMITH~DAVIDLU.

What do you actually want to check. exact match? Then use ==.

Chriss
 
How about
Code:
a = "PETER~SMITH~DAVIDLU~"
b = "DAVIDLU~"
?ALLTRIM(b)$ a &&This returns TRUE

Otherwise search for "T"$ a

Case, content and context

Every string ends in ~ most strings contain T

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
gkratnam,
If you want it to only be true if it matches the full string then instead of $ use == (Exactly Equal).
Note also the way you've written it it is case sensitive.
If you want it to be non-case sensitive then:

? ALLTRIM(UPPER(b)) $ UPPER(a)

Or if exactly equal

? ALLTRIM(UPPER(b)) == ALLTRIM(UPPER(a))




Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS

"I try to be nice, but sometimes my mouth doesn't cooperate.
 
Learned something from All, thanks for your assistance.

First off I made a mistake on the question, my apologies. However, I learned the correct way to do this from Griff's answer.

Code:
a = "PETER~SMITH~DAVIDLU~"
b = "DAVID"  &&I wrote this as "DAVIDLU"
?ALLTRIM(b)$ a

Thanks!
 
Good to know, but you wrote you didn't want TRUE, or was that the desired result?

In fact $ will find both DAVID and DAVIDLU. If you need to asure the name starts this way after a ~ then you could also do

Code:
? ('~'+b) $ a

Which would return .f. for searching 'MI' in '~SMITH' as you actually will search '~MI', so it would only detect names starting with MI in such a tilde separated name list.

But the data itself would need a leading tilde so you also find a name at the start of the string. You don'tneed to store it, but could look for

Code:
? ('~'+b) $ ('~'+a)

That's also what I use in case with comma separated values, ie prefix and suffix a comma to a comma-separated value string and then search for ',searchterm,' to only find rows with that exact full name within the csv list.

Chriss
 
One possible solution that nobody mentioned was OCCURS(). So, [tt]OCCURS(b, a) > 0[/tt] means that b is present in a. (The same considerations of trimming spaces and converting between upper/lower case will apply, of course).

Probably not relevant now that you have found a solution, but worth keeping in mind.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
In the spirit of getting paid by the line of code, here's another:

Code:
lnAt = 0
? "Inefficiency?", is_found_in("inefficiency", "efficiency", @lnAt), lnAt
? "Efficiency?", is_found_in("efficiency", "inefficiency", @lnAt), lnAt
? "Happiness?", is_found_in("happiness", "unhappiness", @lnAt), lnAt


* Call with @lnAt as this is a return parameter
FUNCTION is_found_in
LPARAMETERS tcNeedle, tcHaystack, lnAt

    IF TYPE("tcNeedle") != "C" OR TYPE("tcHaystack") != "C"
        RETURN .F.
    ENDIF

    * Iterate through the haystack looking for the needle
    FOR lnI = 1 TO LEN(tcHaystack) - LEN(tcNeedle) + 1
        * Be optimistic, assume we'll find it
        llFound = .t.

        * Iterate one character by one character to ensure direct comparisonisms
        FOR lnJ = 1 TO LEN(tcNeedle)
            IF SUBSTR(tcHaystack, lnI + lnJ - 1, 1) != SUBSTR(tcNeedle, lnJ, 1)
                * No go.  No good.  No, sir.
                llFound = .f.
                EXIT
            ENDIF
        NEXT

        * Was it our happy thing?
        IF llFound
            * Yes!
            lnAt = lnI
            RETURN .t.
        ENDIF
        * Keep going
    NEXT

    * If we get here, not found
    RETURN .f.

;^)

--
Rick C. Hodgin
 
gkratnam,

even after you correted your question and said Griffs ALLTRIM(b)$ a solves it, I'm not sure if that's what you really want.
Both DAVID and DAVIDLU are found by $. If you want search like a SEEK in character fields does, with a "starts with" match, then at best look for '~DAVID' $ '~PETER~SMITH~DAVIDLU~' and you find that DAVID is in there. Just like you find DAVIDLU in there.

I already also told you this can even be extended to exact matching so DAVID is NOT found, as it's not DAVIDLU. Then you need to search for '~DAVID~' $ '~PETER~SMITH~DAVIDLU~', so it makes a difference what you really want and needs prefixing AND suffixing with the delimiter in both search string and searched string. Because that literally makes the extreme cases to the normal case of finding a full string between two delimiters.

And then $ will only give you the information it's in there, not where. AT() would do that.

Chriss
 
I got the solution from this forum. Yes, this solved it.

Code:
a = "PETER~SMITH~DAVIDLU~"
b = "DAVID~"  &&Added '~' at the end
?ALLTRIM(b)$ a

Learned from Mike, optimal solution is to do '~' at the begging and end. It can handle all the scenario.

Thanks!
 
Thanks Rick for your mini story, it is fun [bigsmile]. It stays fresh in memory when we make it enjoyable.

I may not apply apply this question. But I can definitely make use of it for other purposes.
 
@gkratnam

You should use the delimiter at both ends of the comparision for variable length strings, but you must prefix PETER with one as well, or you would
never be able to search for him. For fixed length strings only the suffix is required...


Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top