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. |
PADL(ALLTRIM(cNumber), LEN(cNumber)) && pads with blanksor
PADL(ALLTRIM(cNumber), LEN(cNumber), "0") && pads with zeroesEither 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 |
?PADL("Original",5)returns
"Origi"
See Also |