Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Search field for string
Message
From
25/08/2010 02:23:12
 
 
To
25/08/2010 01:42:39
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01478292
Message ID:
01478301
Views:
50
>i found the following code finds the word but i would like to limit the response
> to the surrounding 10 words before and 10 words after the KEYWORD is found.
>
>
>do while not eof()
>cfile = rtrim(teach)
>IF 'KEYWORD'$(cFile) = .T.
>? cfile
>endif
>skip
>loop
>enddo
>
>
>k
>
>>at a bit of a loss on where to start on this and can use some expert help.
>>
>>i am looking for a way to search a field for a word.
>>when word is found to show the next 10 letters past the word.
>>
>>Tore gave me some useful search code but i don't know how to apply this to a field - nor how to reveal the search with the 10 letters past
>>
>>'myname'$(cfile) = .T.
>>
>>let's say field is called "myfield" and is about 200 characters in length.
>>the word i am looking for is "myname" placed anywhere in the field.
>>
>>the response once the word is found should reveal "myname 123456789"

Unless I'm misunderstanding what you want to do, you should be able to use the AT() and SUBSTR() functions for this e.g.
lcLookFor = "myname"
lnPrecedingChars = 10
lnFollowingChars = 10

* Within a single row:
lnStartPosition = AT( lcLookFor, MyTable.MyField )

IF lnStartPosition > 0
  * i.e. search expression is found
  * Be sure to handle cases where:
  * 1. The field length is less than LEN( lcLookFor ) + lnPrecedingChars + lnFollowingChars, AND/OR
  * 2. lcLookFor is near the start or end of the field

  * This code just handles the case where the field is plenty large enough, and
  * lcLookFor is comfortably in the middle of the field:
  lcResult = SUBSTR( MyTable.MyField, lnStartPosition - lnPrecedingChars, lnPrecedingChars + LEN( lcLookFor ) + lnFollowingChars )

ELSE
  * search expression not found

ENDIF
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform