INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!

*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.

Jobs

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:

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

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

My Archive

Resources

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