FUNCTION TRIM removes spaces from the ends of a field. FUNCTION TRIM(WS-NAME) drops both ends. FUNCTION TRIM(WS-NAME, LEADING) drops the left side. FUNCTION TRIM(WS-NAME, TRAILING) drops the right side. The field you pass is not changed. You get a new string back.
A name written on a wide card has blank paper on either side of the letters. TRIM cuts off that blank paper. It does not cut spaces that sit between two words. "JANE DOE" stays two words. Only the empty edges go.
The second argument is optional. Leave it off when you want both edges. IBM's examples put a comma before LEADING or TRAILING. Enterprise COBOL for z/OS 6.2 and later has this function. It arrived with APAR PI99703. An older compiler will not know the name.
| Form | What it removes | IBM example |
|---|---|---|
| FUNCTION TRIM(field) | Spaces at both ends | TRIM(" JANE DOE ") is "JANE DOE" |
| FUNCTION TRIM(field, LEADING) | Spaces on the left only | TRIM(" JANE DOE ", LEADING) is "JANE DOE " |
| FUNCTION TRIM(field, TRAILING) | Spaces on the right only | TRIM(" JANE DOE ", TRAILING) is " JANE DOE" |
No second argument. Spaces on the left and the right go. Spaces in the middle stay. FUNCTION TRIM(" Hello, world! ") is "Hello, world!".
Starts at the first character that is not a space and keeps the rest, including any spaces on the right. FUNCTION TRIM(" Hello, world! ", LEADING) is "Hello, world! " with the trailing space still there.
Keeps the left side, spaces included, and stops at the last character that is not a space. FUNCTION TRIM(" Hello, world! ", TRAILING) is " Hello, world!" with the leading space still there.
If the argument is all spaces, or its length is zero, TRIM returns a zero-length value. That is the IBM rule. It is not a single space.
TRIM's result is only as long as the text that remains. A PIC X field has a fixed length. MOVE pads the short result with spaces on the right, so the receiving field looks padded again when you display it. That surprises people who trim trailing spaces and then move the result into another PIC X(20).
When you want the length of the text, nest the calls. FUNCTION LENGTH by itself returns the declared size, so the trailing spaces count. FUNCTION LENGTH(FUNCTION TRIM(WS-NAME)) does not count them.
123456789101112IDENTIFICATION DIVISION. PROGRAM-ID. TRIMDEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NAME PIC X(20) VALUE " JANE DOE". 01 WS-SHOW PIC X(20). 01 WS-LEN PIC 9(4). PROCEDURE DIVISION. MOVE FUNCTION TRIM(WS-NAME) TO WS-SHOW COMPUTE WS-LEN = FUNCTION LENGTH(FUNCTION TRIM(WS-NAME)) STOP RUN.
WS-NAME is 20 characters and is not modified. WS-SHOW receives JANE DOE and is padded back out to 20. WS-LEN is 8, the letters and the space between the names, not 20.
12345601 WS-RAW PIC X(12) VALUE " JANE DOE ". 01 WS-LEFT PIC X(12). 01 WS-RIGHT PIC X(12). PROCEDURE DIVISION. MOVE FUNCTION TRIM(WS-RAW, LEADING) TO WS-LEFT MOVE FUNCTION TRIM(WS-RAW, TRAILING) TO WS-RIGHT
WS-LEFT starts with JANE and still has the two spaces that were on the right of DOE, then MOVE pads the field. WS-RIGHT still has the two spaces in front of JANE. If you display them with a marker on each end, those leftover spaces are visible. A plain DISPLAY hides the difference, because both fields are PIC X(12) after the move.
Use a PIC X(15) field named WS-CARD with VALUE " ADA LOVELACE". Two spaces sit on the left. The literal is 14 characters, so the field pads one space on the right.
LEADING on this value leaves "ADA LOVELACE" plus the spaces that were already on the right. TRAILING leaves the two spaces in front of ADA, because those are not trailing. TRIM with no second argument drops both, so the text is ADA LOVELACE.
1. FUNCTION TRIM(WS-NAME) removes which spaces?
2. FUNCTION TRIM(" JANE DOE ", LEADING) returns:
3. FUNCTION LENGTH(FUNCTION TRIM(WS-NAME)) is useful because: