COBOLintermediate
STRING and UNSTRING Verbs
Understand how COBOL concatenates and splits alphanumeric fields without dynamic string types.
COBOL has no dynamic strings. Every field is a fixed number of bytes, so joining and splitting text needs dedicated verbs.
STRING and UNSTRING are like a luggage packing strip with a fixed number of marked slots — you can pack items end to end or unpack them back out, but the strip never gets longer no matter how much you try to stuff into it.
Key Concepts
1
STRING joins fields into one.
STRING WS-FIRST-NAME DELIMITED BY SPACE
' ' DELIMITED BY SIZE
WS-LAST-NAME DELIMITED BY SPACE
INTO WS-FULL-NAME
WITH POINTER WS-PTR
END-STRING2
DELIMITED BY SPACE stops at the first space, which is how you trim a padded field.
DELIMITED BY SIZE takes the whole thing.
DELIMITED BY SPACEDELIMITED BY SIZE
3
POINTER tracks where the next character goes, so successive STRING statements append rather than
overwrite. It is also the thing people forget to reset:
POINTER
4
MOVE 1 TO WS-PTR <- before every new build5
Leave it where the last iteration finished and the second record's text starts halfway along the field.
6
Clear the receiving field first. STRING writes only as many characters as it produces. Everything beyond
that is whatever was there before:
7
pass 1: "Ravi Kumar "
pass 2: "Asha Rao" + leftovers -> "Asha Raoumar "8
MOVE SPACES TO WS-FULL-NAME before each build. This is the most common bug with this verb, and it
produces plausible-looking wrong output rather than an error.
MOVE SPACES TO WS-FULL-NAME
9
Truncation is silent too. If the target is too short, STRING stops. Use ON OVERFLOW to find out:
ON OVERFLOW
10
STRING ... INTO WS-SHORT-FIELD
ON OVERFLOW DISPLAY 'Name truncated'
END-STRING11
UNSTRING splits one field into several.
UNSTRING WS-CSV-LINE DELIMITED BY ','
INTO WS-FIELD-1 WS-FIELD-2 WS-FIELD-3
TALLYING IN WS-COUNT
END-UNSTRING12
TALLYING counts how many fields were found, which matters when the number of tokens varies — a line with
two commas instead of three leaves the third target holding its previous value unless you check the count.
TALLYING
13
DELIMITED BY ALL ',' treats repeated delimiters as one. Without ALL, a,,b yields three fields with
an empty middle; with it, two. Which behaviour you want depends on the feed, and picking the wrong one
misaligns every field after the gap.
DELIMITED BY ALL ','ALLa,,b
14
Where these verbs get used. Building report lines and messages, and parsing delimited extracts from
open systems — which is exactly where mainframe and non-mainframe systems meet, and where most of these
bugs live.
15
What the interviewer is probing.1. "What does the POINTER phrase do?" Probing: the mechanism. Stalls: "Nothing." Moves up:
it tracks and updates the position in the receiving field, which is how you append across several
STRING statements.
16
2. "Why must you initialise the receiving field first?" Probing: the classic bug. Stalls:
"You need not." Moves up: STRING does not blank the remainder, so leftover characters from a
previous record stay behind.
17
3. "What is the ON OVERFLOW clause for?" Probing: error handling. Stalls: "Nothing." *Moves
up:* detecting that the data did not fit — without it the operation silently truncates.
18
4. "What does DELIMITED BY SIZE mean?" Probing: the detail. Stalls: "The whole field."
Moves up: use the full declared length including trailing spaces, as opposed to stopping at a
delimiter such as a space.