How to Use Wildcards in a String Search RoutineLast reviewed: March 16, 1996Article ID: Q146221 |
The information in this article applies to:
SUMMARYFoxPro includes several functions, such as AT(), that you can use to determine if a string is contained within another string. However, no native function exists that will use wildcards in the string to be searched for in the larger string. This article shows by example how to search for a string containing wildcards within a larger string.
MORE INFORMATION
Sample CodeFUNCTION wcardfind PARAMETER srchstr, wildcard, longstr STORE 0 TO ifoundatSTORE LEN(longstr) TO ilongstrlen STORE LEN(ALLT(srchstr)) TO isrchstrlen STORE ilongstrlen - isrchstrlen TO ilastoffset STORE isrchstrlen - LEN(ALLT(STRTRAN(srchstr, wildcard, ""))) ; TO inumwcardsSTORE .F. TO lfound STORE 0 TO isrchoffsetSTORE (ilongstrlen > isrchstrlen) AND ; (LEN(wildcard)=1) AND (isrchstrlen>0) TO okDO WHILE (! isrchoffset > ilastoffset) AND (! lfound) AND ok STORE 0 TO imatch FOR LOOP = 1 TO isrchstrlen IF ASC(SUBSTR(srchstr,LOOP,1)) = ; ASC(SUBSTR(longstr,LOOP+isrchoffset,1)) imatch=imatch+1 ENDIF ENDFOR IF imatch+inumwcards = isrchstrlen lfound = .T. ifoundat = isrchoffset+1 ELSE isrchoffset=isrchoffset+1 ENDIFENDDO RETURN IIF(lfound,ifoundat,0)
Testing the FunctionPlace the following code before the above function in a program file to test the function. The value returned in the wait window should be 4:
x1="d?f" x2="?" x3="abcdefgh" xresult=wcardfind(x1,x2,x3) wait wind str(xresult) |
Additional reference words: 3.00 3.00b VFoxWin
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |