Left(), Right(), SubStr()

These functions let you pull apart character strings. LEFT() grabs a specified number of characters from the beginning; RIGHT() takes the specified number from the end. SUBSTR() is the most general, but also the most difficult to use; it lets you extract a consecutive string of characters from anywhere in the original string. You can combine these functions with AT() (or similar search functions) to parse a string.

Usage

cReturnValue = LEFT( cString, nCharacters )
LEFT() returns the first nCharacters characters of cString.

Example

? LEFT("Hacker's Guide", 8)   && Returns "Hacker's"

Usage

cReturnValue = RIGHT( cString, nCharacters )
RIGHT() returns the last nCharacters of cString. But watch out for trailing blanks. RIGHT() uses the string you give it and doesn't remove trailing blanks. You'll probably want to TRIM() the string before you apply RIGHT().

Example

? RIGHT("Hacker's Guide", 5)   && Returns "Guide"
? RIGHT("This string has 2 trailing blanks  ", 2)
    && returns "  "

Usage

cReturnValue = SUBSTR( cString, nStart [ , nLength ] )

Parameter

Value

Meaning

cString

Character

The string from which a portion is to be extracted.

nStart

Numeric

The position of the first character to extract.

nLength

Numeric

The number of characters to extract. If there are fewer than nLength characters from nStart to the end of the string, all remaining characters are returned.

Omitted

All characters from nStart to the end of the string are extracted.


In versions of VFP through 5.0a, SUBSTR() does one truly odd thing. Its behavior if nStart is greater than the length of the string depends on the current value of SET TALK. Really! With SET TALK OFF, you simply get the empty string. But with SET TALK ON, attempting to start a SUBSTR() after the end of the character string gives an error message of "Beyond String." Try the following in an older version:

SET TALK OFF
? SUBSTR('abc', 4)
SET TALK ON
? SUBSTR('abc', 4)
Why two different behaviors? The folks at Microsoft say it's an attempt to be forgiving. If TALK is OFF, you're probably in a program and don't want it to crash it. But if TALK is ON, you're probably working interactively and need to know where you went wrong. Nice try, but we'd prefer consistency and apparently, finally, so does Microsoft. Starting in VFP 6, SUBSTR() accepts a value of nStart beyond the end of cString regardless of the setting of TALK.

Example

? SUBSTR("Visual FoxPro", 9, 2)   && Returns "ox"
? SUBSTR("Visual FoxPro", 8)      && Returns "FoxPro"
* Line below returns "Fox"
? SUBSTR("Visual FoxPro", AT(" ", "Visual FoxPro") + 1,3)
All three of these functions have double-byte counterparts for working in languages like Chinese: LeftC(), RightC(), and SubStrC().

See Also

At(), AtC(), LeftC(), Occurs(), RAt(), RightC(), Set Talk, Stuff(), SubStrC(), Trim()


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .