Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

PADC(), PADL(), PADR()

These functions pad strings with a specified character. The final letter of each function indicates where the padding occurs: C for center, L for left, and R for right.

Usage

cPaddedString = PADC( eExpr, nLength, cPadCharacter )
cPaddedString = PADL( eExpr, nLength, cPadCharacter )
cPaddedString = PADR( eExpr, nLength, cPadCharacter )

Parameter

Value

Meaning

eExpr

Character, Memo, Numeric, Float, Currency, Double, Integer, Date or DateTime

The item to be padded.

 nLength

Numeric

The length the string should be after padding.

cPadCharacter

Character

The character to use for padding.

Omitted

Spaces (CHR(32)) are used for padding.

Many folks don’t notice the third parameter here and think these functions can only be used to pad with blanks. cPadCharacter can be any character at all, so you can use these functions to fill a money amount with “*”s or add a dot leader to an item in a report.

Another thing most folks miss is that you can pass more than just character strings to the PADx functions without first converting them. The conversion is automatic. The number, date or datetime is converted so that it occupies the minimum space possible, then padding is applied.

PADL() is particularly useful when dealing with character strings composed entirely of digits. With PADL(), it’s easy to keep them right-justified so they sort properly. If numeric character strings are left-justified, they sort wrong. (For an example, take a look at the SYS() Functions in the FoxPro Help index— they list as SYS(1), SYS(10), SYS(11), SYS(100), … , SYS(2), and so forth.) It’s easy to make them sort properly though—just use:

PADL(ALLTRIM(cNumber), LEN(cNumber))       && pads with blanks

or

PADL(ALLTRIM(cNumber), LEN(cNumber), "0")  && pads with zeroes

Either approach yields proper sorting.

PADC() is handy in reporting when you want to center an expression, but the expression length may vary. Apply PADC(), using the size of the allocated space as the length.

Example

PROCEDURE GetId(cTable)
* Get a new unique id for the specified table
* assumes next id for each table is stored in IdTable

LOCAL nOldWorkArea, cOldOrder, lWasUsed, cRetValue

nOldWorkArea = SELECT()
IF USED("IdTable")
   lWasUsed=.T.
   SELECT IdTable
   cOldOrder=ORDER()
ELSE
   lWasUsed=.F.
   SELECT 0
   USE IdTable
ENDIF

SET ORDER TO TableName

* Find this table
SEEK cTable

IF FOUND()
   * lock it, grab it and update it
   DO WHILE NOT RLOCK()
   ENDDO

   cRetValue=NextId
   REPLACE NextId WITH ;
           PADL(INT(VAL(cRetValue))+1,;
           LEN(NextId),"0")
   UNLOCK RECORD RECNO()
ELSE
   cRetValue="0000"
ENDIF

IF lWasUsed
   SET ORDER TO (cOldOrder)
ELSE
   USE IN IdTable
ENDIF

SELECT (nOldWorkArea)
ENDIF

RETURN cRetValue

The function shown here returns the next available ID for a specified table. It uses PADL() to ensure that the ID has the right number of digits. It assumes you have a table with two fields, TableName and NextId, with one record for each table in your application. It finds and locks the record for the specified table, grabs NextId, then computes the next NextId before unlocking the record.

There is one thing to watch out for with the PADx() functions. If nLength is less than the current length of cString, all three functions truncate the original string from the right. For example:

?PADL("Original",5)

returns

"Origi"

See Also

Len(), Replicate()