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 ) |
Example |
? LEFT("Hacker's Guide", 8) && Returns "Hacker's" |
Usage |
cReturnValue = RIGHT( cString, nCharacters ) |
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) |
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) |
See Also |
At(), AtC(), LeftC(), Occurs(), RAt(), RightC(), Set Talk, Stuff(), SubStrC(), Trim() |