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!

ATXLEFT() & ATXRIGHT(): Two useful UDFs for parsing strings.

String Commands

ATXLEFT() & ATXRIGHT(): Two useful UDFs for parsing strings.

by  mmerlinn  Posted    (Edited  )
It seems that when processing text data one of the most common things we do is to break up strings into its significant parts.

And when doing this, we invariably wind up with typos and other errors because of the way FoxPro requires us to write our code. After losing all of my hair chasing bugs, I finally decided that it would be easier to let the program do what it does best, compute. Below are two of the results and an example on how to use them.

There is undoubtedly a processing time penalty when the program is doing heavy duty string processing, but for normal use these UDFs should work with no noticeable time lag in most situations.

These functions exclude the search string from the results. If you need to include the search string in the result, you could easily modify these toward that end.

************************************************************
NOTE: If these are not working for any reason, please closely examine this code for extra spaces, i.e. 'z str' instead of 'zstr'. For some reason when I save this the saving program or receiving program is inserting random spaces, some of which make this code fail when run.

************************************************************

[TT][COLOR BLUE]
******************************
*
* FUNCTION NAME: ATXRIGHT
*
******************************
*
* Get right part of string starting at search string exclusive
*
* Copyright 2005 by mmerlinn, POB 1090, Rainier, OR 97048
*
* This function may be used by anyone provided that
* everything, including this header, is included
* in the user's program.
*
******************************
*
*
* SYNTAX:
* = ATXRIGHT(exprC_1|exprN_1|exprD_1[, exprC_2[, exprC_3|exprN_2[, exprC_3|exprN_3[, exprN_3]]]])
*
* OPTIONS:
* 1) = ATXRIGHT(exprC_1)
* 2) = ATXRIGHT(exprC_1, exprC_2)
* 3) = ATXRIGHT(exprC_1, exprC_2, exprN_2)
* 4) = ATXRIGHT(exprC_1, exprC_2, exprC_3)
* 5) = ATXRIGHT(exprC_1, exprC_2, exprC_3, exprN_3)
* 6) = ATXRIGHT(exprC_1, exprC_2, exprN_2, exprN_3)
* 7) = ATXRIGHT(exprC_1, exprC_2, exprN_2, exprC_3)
* 8) = ATXRIGHT(exprC_1, exprC_2, exprN_2, exprC_3, exprN_3)
*
* 1) = ATXRIGHT(exprD_1)
* 2) = ATXRIGHT(exprD_1, exprC_2)
* 3) = ATXRIGHT(exprD_1, exprC_2, exprN_2)
* 4) = ATXRIGHT(exprD_1, exprC_2, exprC_3)
* 5) = ATXRIGHT(exprD_1, exprC_2, exprC_3, exprN_3)
* 6) = ATXRIGHT(exprD_1, exprC_2, exprN_2, exprN_3)
* 7) = ATXRIGHT(exprD_1, exprC_2, exprN_2, exprC_3)
* 8) = ATXRIGHT(exprD_1, exprC_2, exprN_2, exprC_3, exprN_3)
*
* 1) = ATXRIGHT(exprN_1)
* 2) = ATXRIGHT(exprN_1, exprC_2)
* 3) = ATXRIGHT(exprN_1, exprC_2, exprN_2)
* 4) = ATXRIGHT(exprN_1, exprC_2, exprC_3)
* 5) = ATXRIGHT(exprN_1, exprC_2, exprC_3, exprN_3)
* 6) = ATXRIGHT(exprN_1, exprC_2, exprN_2, exprN_3)
* 7) = ATXRIGHT(exprN_1, exprC_2, exprN_2, exprC_3)
* 8) = ATXRIGHT(exprN_1, exprC_2, exprN_2, exprC_3, exprN_3)
*
* EXAMPLES keyed to above OPTIONS (Where a = ' george washington '):
*
* 1) ATXRIGHT(a) returns ""
*
* 2) ATXRIGHT(a, 'g') returns 'eorge washington'
* 2) ATXRIGHT(a, 'a') returns 'shington'
* 2) ATXRIGHT(a, 'wash') returns 'ington'
*
* 3) ATXRIGHT(a, 'g', 0) returns 'george washington'
* 3) ATXRIGHT(a, 'g', 1) returns 'eorge washington'
* 3) ATXRIGHT(a, 'g', 2) returns 'e washington'
* 3) ATXRIGHT(a, 'n', 1) returns 'gton'
*
* 4) ATXRIGHT(a, 'g', 'g') returns 'eor'
* 4) ATXRIGHT(a, 'g', 's') returns 'eorge wa'
* 4) ATXRIGHT(a, 'geo', 'o') returns 'rge washingt'
*
* 5) ATXRIGHT(a, 'g', 'g', 0) returns ""
* 5) ATXRIGHT(a, 'g', 'g', 1) returns 'eor'
* 5) ATXRIGHT(a, 'g', 'g', 2) returns 'eorge washin'
* 5) ATXRIGHT(a, 'g', 'g', 3) returns 'eorge washington'
* 5) ATXRIGHT(a, 'g', 'ge', 1) returns 'eor'
*
* 6) ATXRIGHT(a, 'g', 0, 0) returns ""
* 6) ATXRIGHT(a, 'g', 0, 1) returns ""
* 6) ATXRIGHT(a, 'g', 0, 2) returns 'geor'
* 6) ATXRIGHT(a, 'g', 0, 3) returns 'george washin'
* 6) ATXRIGHT(a, 'g', 1, 2) returns 'eorge washin'
* 6) ATXRIGHT(a, 'ge', 1, 1) returns 'or'
*
* 7) ATXRIGHT(a, 'g', 0, 'g') returns ""
* 7) ATXRIGHT(a, 'g', 0, 'n') returns 'george washi'
* 7) ATXRIGHT(a, 'g', 2, 'g') returns 'e washin'
* 7) ATXRIGHT(a, 'g', 2, 'o') returns 'e washingt'
* 7) ATXRIGHT(a, 'ge', 2, 'o') returns ' washingt'
*
* 8) ATXRIGHT(a, 'g', 0, 'o', 0) returns ""
* 8) ATXRIGHT(a, 'g', 0, 'o', 1) returns 'ge'
* 8) ATXRIGHT(a, 'g', 0, 'o', 2) returns 'george washingt'
* 8) ATXRIGHT(a, 'g', 0, 'o', 3) returns 'george washington'
* 8) ATXRIGHT(a, 'ge', 0, 'o', 2) returns 'george washingt'
*
* 8) ATXRIGHT(a, 'g', 1, 'o', 0) returns ""
* 8) ATXRIGHT(a, 'g', 1, 'o', 1) returns 'e'
* 8) ATXRIGHT(a, 'g', 1, 'o', 2) returns 'eorge washingt'
* 8) ATXRIGHT(a, 'g', 1, 'o', 3) returns 'eorge washington'
* 8) ATXRIGHT(a, 'ge', 1, 'o', 2) returns 'orge washingt'
*
* 8) ATXRIGHT(a, 'g', 2, 'o', 0) returns ""
* 8) ATXRIGHT(a, 'g', 2, 'o', 1) returns 'e washingt'
* 8) ATXRIGHT(a, 'g', 2, 'o', 2) returns 'e washington'
* 8) ATXRIGHT(a, 'ge', 2, 'on', 2) returns ' washington'
*
* 8) ATXRIGHT(a, 'e', 0, 'o', 1) returns 'ge'
* 8) ATXRIGHT(a, 'e', 0, 'o', 2) returns 'george washingt'
* 8) ATXRIGHT(a, 'e', 1, 'o', 2) returns 'orge washingt'
* 8) ATXRIGHT(a, 'eo', 1, 'eo', 2) returns 'rge washington'
*
* OTHER UDFs CALLed:
* ATXLEFT()
*
* CALLed by UDFs:
* FMT()
*
* ENTRY:
* exprC_1 C: Original string
* exprC_2 C: First string to search for (OPTIONAL)
* exprC_3 C: Second string to search for (OPTIONAL), defaults to exprC_2
* exprD_1 D: Original date
* exprN_1 N: Original number
* exprN_2 N: Relative postion of first string to search for (OPTIONAL), defaults to 1
* Counts from beginning of ALLTRIMmed string
* exprN_3 N: Relative position of second string to search for (OPTIONAL), defaults to 1
* Counts from first character AFTER exprN_2 occurance of exprC_2
*
* EXIT:
*
* RETURNs:
* STRING beginning at first character AFTER exprN_2 occurance of exprC_2
* and ending at last character BEFORE exprN_3 occurance of exprC_3
* or ending at end of ALLTRIMmed original expression
*
* Trips error routine if original expression is not a string, date, or number
* Defaults to empty string if exprC_2 is missing
* Defaults to empty string if exprC_2 is not a character string
* Defaults to empty string if exprC_2 is at end of original expression
* Defaults to empty string if exprC_2 is not found
* Otherwise returns a string beginning at end of search string, BUT
* does NOT trim LEADING spaces from resulting string
*
******************************

PARAMETERS qExpr, qSearch, qVar3, qVar4, qVar5
PRIVATE ALL LIKE z*

ztype = TYPE('qExpr')
IF ztype $ 'CDMN'

DO CASE
CASE ztype = 'N' && Convert numeric expression to a string
qExpr = STR(qExpr)
CASE ztype = 'D' && Convert date expression to a string
qExpr = DTOC(qExpr) && Date format controlled by SET CENTURY and SET DATE
ENDCASE

qExpr = ALLTRIM(qExpr)
zstr = "" && Defaults to empty string
IF TYPE('qSearch') = 'C'
zcount = IIF(TYPE('qVar3') = 'N', IIF(qVar3 < 0, 0, INT(qVar3)), 1)
IF EMPTY(zcount)
zstr = qExpr
ELSE
z = AT(qSearch, qExpr, zcount)
DO CASE
CASE EMPTY(z)
&& Search string not found
CASE z + LEN(qSearch) - 1 <> LEN(qExpr)
&& Search string found before end of string
zstr = SUBSTR(qExpr, z + LEN(qSearch))
ENDCASE
ENDIF
IF NOT EMPTY(zstr)
&& Get left part of resulting string using optional parameters
DO CASE
CASE TYPE('qVar3') = 'C'
zstr = ATXLEFT(zstr, qVar3, qVar4)
CASE TYPE('qVar4') = 'C'
zstr = ATXLEFT(zstr, qVar4, qVar5)
CASE TYPE('qVar4') = 'N'
zstr = ATXLEFT(zstr, qSearch, qVar4)
ENDCASE
ENDIF
ENDIF
ELSE && Trip error routine
mmm = Invalid_Input && Change this to however you handle errors
ENDIF

RETURN zstr
[/COLOR][/TT]

=============================================================================

[TT][COLOR BLUE]
******************************
*
* FUNCTION NAME: ATXLEFT
*
******************************
*
* Get left part of string ending at search string exclusive
*
* Copyright 2006 by mmerlinn, POB 1090, Rainier, OR 97048
*
* This function may be used by anyone provided that
* everything, including this header, is included
* in the user's program.
*
******************************
*
*
* SYNTAX:
* = ATXLEFT(exprC_1|exprN_1|exprD_1[, exprC_2[, exprN_2]])
*
* OPTIONS:
* 1) = ATXLEFT(exprC_1)
* 2) = ATXLEFT(exprC_1, exprC_2)
* 3) = ATXLEFT(exprC_1, exprC_2, exprN_2)
*
* 1) = ATXLEFT(exprD_1)
* 2) = ATXLEFT(exprD_1, exprC_2)
* 3) = ATXLEFT(exprD_1, exprC_2, exprN_2)
*
* 1) = ATXLEFT(exprN_1)
* 2) = ATXLEFT(exprN_1, exprC_2)
* 3) = ATXLEFT(exprN_1, exprC_2, exprN_2)
*
* EXAMPLES keyed to above OPTIONS (Where a = ' george washington '):
*
* 1) ATXLEFT(a) returns 'george washington'
*
* 2) ATXLEFT(a, 'g') returns ""
* 2) ATXLEFT(a, 'a') returns 'george w'
* 2) ATXLEFT(a, 'wash') returns 'george '
*
* 3) ATXLEFT(a, 'g', 0) returns ""
* 3) ATXLEFT(a, 'g', 1) returns ""
* 3) ATXLEFT(a, 'g', 2) returns 'geor'
* 3) ATXLEFT(a, 'g', 3) returns 'george washin'
*
* 3) ATXLEFT(a, 'o', 0) returns ""
* 3) ATXLEFT(a, 'o', 1) returns 'ge'
* 3) ATXLEFT(a, 'o', 2) returns 'george washingt'
* 3) ATXLEFT(a, 'o', 3) returns 'george washington'
*
* OTHER UDFs CALLed:
* (None)
*
* CALLed by UDFs:
* ATXRIGHT()
* FMT()
*
* ENTRY:
* exprC_1 C: Original string
* exprC_2 C: String to search for (OPTIONAL)
* exprD_1 D: Original date
* exprN_1 N: Original number
* exprN_2 N: Occurance of string to search for (OPTIONAL), defaults to 1
*
* EXIT:
*
* RETURNs:
* STRING beginning at first non-space character of original expression
* and ending one byte (inclusive) before exprN_2 occurance of exprC_2
*
* Trips error routine if original expression is not a string, date, or number
* Defaults to ALLTRIMmed original expression if exprC_2 is missing
* Defaults to ALLTRIMmed original expression if exprC_2 is not a character string
* Defaults to ALLTRIMmed original expression if exprC_2 is not found
* Defaults to empty string when exprC_2 is at beginning of original expression
* Defaults to empty string when exprN_2 is less than 1
* Otherwise returns string ending at exprC_2 (exclusive), BUT
* does NOT trim TRAILING spaces from resulting string
*
******************************

PARAMETERS qExpr, qSearch, qCount
PRIVATE ALL LIKE z*

ztype = TYPE('qExpr')

IF ztype $ 'CDMN'

DO CASE
CASE ztype = 'N'
&& Convert numeric expression to a string
qExpr = STR(qExpr)
CASE ztype = 'D'
&& Convert date expression to a string
&& Date format controlled by SET CENTURY and SET DATE
qExpr = DTOC(qExpr)
ENDCASE

qExpr = ALLTRIM(qExpr)
zstr = qExpr && Defaults to ALLTRIMmed original expression
IF TYPE('qSearch') = 'C'
zcount = IIF(TYPE('qCount') = 'N', IIF(qCount < 0, 0, INT(qCount)), 1)
IF EMPTY(zcount)
zstr = ""
ELSE
z = AT(qSearch, qExpr, zcount)
DO CASE
CASE EMPTY(z)
&& Search string not found
CASE EMPTY(z - 1)
&& Search string found at beginning of original expression
zstr = ""
OTHERWISE
&& Search string found after beginning of original expression
zstr = LEFT(qExpr, z - 1)
ENDCASE
ENDIF
ENDIF
ELSE && Trip error routine
mmm = 'Invalid_Input' && Change this to however you handle errors
ENDIF

RETURN zstr
[/COLOR][/TT]


*************************************************************

Example #1:

cString = "Martin Luther King Jr"
cSeparator = " "
DO WHILE NOT EMPTY(cString)
cWord = ATXLEFT(cString, cSeparator)
cString = ATXRIGHT(cString, cSeparator)
?"-"
?cWord
?"-"
?CHR(13)
ENDDO

Prints:[color blue]
-Martin-
-Luther-
-King-
-Jr-[/color]


*************************************************************

Example #2:

cString = "Martin Luther King Jr"
cSeparator = " "
nCount = 0
DO WHILE NOT EMPTY(cString)
nCount = nCount + 1
DIMENSION aWord(nCount)
aWord(nCount) = ATXLEFT(cString, cSeparator)
cString = ATXRIGHT(cString, cSeparator)
ENDDO

Results:[color blue]
aWord(1) = "Martin"
aWord(2) = "Luther"
aWord(3) = "King"
aWord(4) = "Jr"[/color]


*************************************************************

Example #3

cStr = '"General Period" && Two spaces
cSearch = "er"

? "-" + ATXLEFT(cStr, cSearch) + "-"

Prints:[color blue]
-Gen-[/color]



? "-" + ATXRIGHT(cStr, cSearch) + "-"

Prints:[color blue]
-al period-[/color]


cSearch = " " && One space

? "-" + ATXLEFT(cStr, cSearch) + "-"

Prints:[color blue]
-General-[/color]



? "-" + ATXRIGHT(cStr, cSearch) + "-"

Prints:[color blue]
- period-[/color]
&& Note leading space
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top