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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SUBSTR() that also works from the right

Usefull Functions & Procedures

SUBSTR() that also works from the right

by  Neil Toulouse  Posted    (Edited  )
A new SUBSTR() function that works from both left and right, giving you appropriate values depending on the parameters passed.

Thanks go to OlafDoschke for the idea and to CraigSBoyd for tidying it all up!

SUBSTREX(cExpression, nStartPosition [, nCharactersReturned] [, lAlltrim])

If you pass positive numbers you will get the same result as the SUBSTR() function.

Passing a negative number for nStartPoition will read the start position from the right.

Passing a negative number for nCharactersReturned will read the characters right to left.

The lAlltrim parameter dictates whether or not cExpression is ALLTRIM'ed before the result is derived.

For example:

Code:
? SUBSTREX( 'TOULOUSE', 4, 1) && returns "L"
Code:
? SUBSTREX( 'TOULOUSE', 4, -2) && returns "LU"
Code:
? SUBSTREX( 'TOULOUSE', -4, 2) && returns "OU"
Code:
? SUBSTREX( 'TOULOUSE', -4, -2) && returns "OL"
Code:
? SUBSTREX( 'TOULOUSE', 2) && returns "OULOUSE"
Code:
? SUBSTREX( 'TOULOUSE', -3) && returns "USE"
Code:
? SUBSTREX( ' TOULOUSE', 5, -5, .F. ) && returns "LUOT "
Code:
? SUBSTREX( ' TOULOUSE', 5, -5, .T. ) && returns "LUOT"

Here is the function:

Code:
FUNCTION SUBSTREX()
    LPARAMETERS tcString, tnPos, tnLen, tlAllTrim
    LOCAL lcResult, lnFor, lcBitOfString

    lcResult = ""

    *** Check Parms
    IF TYPE('tcString') = 'C' AND TYPE('tnPos') = 'N' AND (TYPE('tnLen') = 'L' OR TYPE('tnLen') = 'N' ) AND Type('tlAlltrim') = 'L'
        IF tlAllTrim
            tcString = ALLTRIM(tcString)
        ENDIF
        IF tnPos < 0
            tnPos = LEN(tcString) - ABS(tnPos) + 1
        ENDIF
        DO CASE
            CASE TYPE('tnLen') = 'L'
                *** Return all from starting position if no second parm is passed
                lcResult = SUBSTR( tcString, tnPos)
            CASE tnLen > 0
                *** Standard SUBSTR() - Read left to right
                lcResult = SUBSTR(tcString, tnPos, tnLen)
            CASE tnLen < 0
                tnLen = ABS(tnLen)
                IF tnLen > tnPos
                    tnLen = tnPos
                ENDIF
                *** Read from left for starting position, read right to left for result
                lcBitOfString = SUBSTR(tcString, tnPos - tnLen + 1, tnLen)
                FOR lnFor = tnLen TO 1 STEP -1
                    lcResult = lcResult + SUBSTR(lcBitOfString, lnFor, 1)
                ENDFOR
        ENDCASE
    ENDIF
    RETURN lcResult
ENDFUNC
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