Functions for processing text in FoxPro

FoxPro has always had a wide range of commands and functions for processing text - more than are strictly necessary for a database language. This is a list of some of the more useful commands and functions. Many of them can accept extra optional parameters. Look in the FoxPro Help system for more information.

The distinction between commands and functions is that commands do something whereas functions just return a value to be displayed or used in another command.

Note that functions are always followed by a pair of brackets to hold the parameters - even if there's no parameter required. Unlike Visual Basic, FoxPro does not use the "$" suffix in functions to force them to return a string.

<text1> + <text2> return <text1text2>
<text1> - <text2> return <text1text2> with any spaces from <text1> removed and added to the end of the result. An unusual function but sometimes useful.
<text1> $ <text2> return .T. if <text1> exists in <text2>
ALLTRIM(<text>) return <text> with leading and trailing spaces removed. In VFP 9 and above this can remove characters other than space.
AT(<char>,<text>) return the position of character <char> in string <text> or 0 if <char> is not found. The RAT() function does the same but starting at the end of the target string <text>.
ATC(<char>,<text>) identical to AT but is not case-sensitive.
CTOD(<text>) return <text> as a variable of type date. Note that CTOD will fail if STRICTDATE is set to 2.
DTOC(<date>) return <date> as a variable of type string in 'dd/mm/yy' or 'dd/mm/yyyy' format depending on the state of SET CENTURY and SET DATE.
DTOS(<date>) return <date> as a variable of type string in 'yyyymmdd' format. Use DTOS() when you are wanting to index on an expression like Surname + DTOS(DateOfBirth).
FILETOSTR(<filename>) return a string holding the contents of the file named <filename>. Useful in combination with STRTOFILE because you can read a file from disk into memory, process it, and then write it back out to disk. The only limitation in the 16Mb limit on FoxPro string variables.
ISALPHA(<text>) return .T. if the first character of <text> is numeric.
ISLOWER(<text>) return .T. if the first character of <text> is lower case.
ISUPPER(<text>) return .T. if the first character of <text> is upper case.
LEFT(<text>, <n>) return the leftmost <n> characters of <text>.
LEN(<text>) return the length of <text> - including spaces. Remember this when you are getting the length of data retrieved from a field.
LIKE(<text1>, <text2>) compares <text1> and <text2> letter by letter and returns .T. if they match. <text1> can include the wild cards '*' and '?'.
LOWER(<text>) return <text> as lowercase text.
LTRIM(<text>) return <text> with leading spaces removed.
OCCURS(<text1>, <text2>) return the number of times that the <text1> occurs in <text2>.
PADC(<text>, <n>, <char>) pad <text> to <n> characters long by adding <char> to both ends.
PADL(<text>, <n>, <char>) pad <text> to <n> characters long by adding <char> to the start.
PADR(<text>, <n>, <char>) pad <text> to <n> characters long by adding <char> to the end.
PROPER(<text>) return <text> in lowercase text with the initial letters capitalised. Not as useful as you might think because many proper names include a mixture of uppercase and lowercase letters like "McDonald" and "IBM".
RIGHT(<text>, <n>) return the rightmost <n> characters of <text>. Remember that this will include trailing spaces.
RTRIM(<text>) return <text> with trailing spaces removed.
STR(<number>,<m>,<n>) return <number> as a string variable of <m> characters rounded to <n> decimal places.
STREXTRACT(<text>, <begin>, <end>) returns the characters between the delimiters <begin> and <end> inside <text>. Additional parameters let you distinguish between multiple occurences of the delimiters and choose whether the delimiters are included in the text which is returned. Very useful when processing HTML and XML.
STRTOFILE(<filename>) write a string to a file and return the number of bytes written. Useful in combination with FILETOSTR because you can read a file from disk into memory, process it, and write it back out to disk.
STRTRAN(<text>, <a>, <b>) replace every occurrence of <a> inside <text> with another character or characters <b>.
STUFF(<text>, <m>, <n>, <a>) replace <n> characters from position <m> in <text> with the character or characters <a>.
TRIM(<text>) identical to RTRIM.
SUBSTR(<text>,<m>,<n>) return <n> characters from within <text> starting at character <m>.
UPPER(<text>) return <text> in upper case text.

We have a page showing the comparison between VFP and VBA text functions.

Access Tips

FoxPro Tips

General Tips

 

Related Items

FoxPro Functions

FoxPro has always had functions to read and write files at a low level.

Read More

Useful Visual FoxPro Commands

Description of useful FoxPro commands and functions

Read More

American date formats used by Access SQL

Access uses an American date format in SQL commands

Read More

Functions for file names in FoxPro

FoxPro functions to manipulate names of files and folders

Read More

Functions for manipulating tables in FoxPro

Commands and functions to manipulate tables in FoxPro

Read More