Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: FoxPro FAQ

Usefull Functions & Procedures

SUBSTR() that also works from the right by Neil Toulouse
Posted: 24 Jun 05 (Edited 19 Jul 05)

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:


? SUBSTREX( 'TOULOUSE', 4, 1) && returns "L"


? SUBSTREX( 'TOULOUSE', 4, -2) && returns "LU"


? SUBSTREX( 'TOULOUSE', -4, 2) && returns "OU"


? SUBSTREX( 'TOULOUSE', -4, -2) && returns "OL"


? SUBSTREX( 'TOULOUSE', 2) && returns "OULOUSE"


? SUBSTREX( 'TOULOUSE', -3) && returns "USE"


? SUBSTREX( ' TOULOUSE', 5, -5, .F. ) && returns "LUOT "


? SUBSTREX( ' TOULOUSE', 5, -5, .T. ) && returns "LUOT"

Here is the function:


    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)
        IF tnPos < 0
            tnPos = LEN(tcString) - ABS(tnPos) + 1
        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
                *** 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)
    RETURN lcResult

Back to Microsoft: FoxPro FAQ Index
Back to Microsoft: FoxPro Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close