STRING and UNSTRING
Two COBOL statements: STRING joins several fields into one, and UNSTRING splits one field into several.
Also written STRING, UNSTRING, DELIMITED BY, WITH POINTER, TALLYING IN
STRING joins pieces of text together. STRING WS-FIRST DELIMITED BY SPACE, ' ' DELIMITED BY SIZE, WS-LAST DELIMITED BY SPACE INTO WS-FULL-NAME builds a full name from two fields. DELIMITED BY SPACE takes each field up to its first space, which removes trailing blanks. DELIMITED BY SIZE takes the whole field.
UNSTRING does the opposite. UNSTRING WS-LINE DELIMITED BY ',' INTO WS-ACCT WS-NAME WS-AMOUNT splits a line of comma-separated values into three fields. Options can count how many fields were filled with TALLYING IN, or record how long each piece was with COUNT IN.
Both statements can use WITH POINTER, a field that says where in the text to start, which lets a program build or read text in several steps. ON OVERFLOW runs code when the receiving field is too small or there are more pieces than fields.
A common mistake is forgetting to clear the receiving field first. STRING only writes the characters it builds, so anything left from before stays at the end. Moving spaces to the field before the STRING avoids it.
Where can I learn this hands-on?
Right here, at ZedBank. You take a junior job at a simulated bank and learn each part of the work by doing it, with Zed beside you. There's nothing to install.
Start at ZedBankRelated
- MOVEThe COBOL statement that copies a value into a field, converting it to fit the receiving field's format.
- INSPECTA COBOL statement that counts, replaces or converts characters inside a field.
- WORKING-STORAGEThe section of a COBOL program that declares its internal variables: counters, totals, flags and work areas.
- PICTURE clauseThe COBOL declaration that says what kind of data a field holds and how long it is, written as PIC.
- PROCEDURE DIVISIONThe part of a COBOL program that contains the actual logic, the statements that run.