TExcelTAD
Home › COBOL › COBOL STRING & UNSTRING

COBOL STRING & UNSTRING

Text fields in COBOL have a fixed length and are padded with spaces, so joining and splitting text needs a little more care than in other languages. Two verbs do the work: STRING joins pieces into one field and UNSTRING splits one field into several.

STRING

cobol
           STRING source-1 DELIMITED BY delimiter
                  source-2 DELIMITED BY SIZE
                  ...
               INTO target
               [WITH POINTER ptr]
               [ON OVERFLOW ...]
           END-STRING

Each source is copied until its delimiter is found (the delimiter itself is not copied). DELIMITED BY SIZE copies the whole source. DELIMITED BY SPACE stops at the first space, which is the usual way to drop the padding of a fixed-length field.

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. STRING-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-FIRST           PIC X(10)   VALUE "Grace".
       01  WS-LAST            PIC X(10)   VALUE "Hopper".
       01  WS-FULL            PIC X(30).
       01  WS-EMAIL           PIC X(30).
       PROCEDURE DIVISION.
           STRING WS-FIRST DELIMITED BY SPACE
                  " "      DELIMITED BY SIZE
                  WS-LAST  DELIMITED BY SPACE
               INTO WS-FULL
           END-STRING
           DISPLAY "Full name: [" WS-FULL "]"
           STRING FUNCTION LOWER-CASE(WS-FIRST) DELIMITED BY SPACE
                  "."                           DELIMITED BY SIZE
                  FUNCTION LOWER-CASE(WS-LAST)  DELIMITED BY SPACE
                  "@example.com"                DELIMITED BY SIZE
               INTO WS-EMAIL
           END-STRING
           DISPLAY "Email:     " WS-EMAIL
           STOP RUN.
Full name: [Grace Hopper                  ]
Email:     grace.hopper@example.com

Watch out: STRING does not clear the target first. Characters beyond the joined text keep their old content. MOVE SPACES TO target before a STRING unless the target is freshly initialised.

Names with spaces

DELIMITED BY SPACE stops at the first space, so "Mary Ann" would become "Mary". Use a two-character delimiter such as " " (two spaces), or FUNCTION TRIM, when values can contain single spaces:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. STRING-SPACES.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-CITY            PIC X(15)   VALUE "San Jose".
       01  WS-STATE           PIC X(5)    VALUE "CA".
       01  WS-OUT-1           PIC X(30).
       01  WS-OUT-2           PIC X(30).
       PROCEDURE DIVISION.
           STRING WS-CITY DELIMITED BY SPACE
                  ", " WS-STATE DELIMITED BY SIZE
               INTO WS-OUT-1
           END-STRING
           STRING WS-CITY DELIMITED BY "  "
                  ", " DELIMITED BY SIZE
                  WS-STATE DELIMITED BY SPACE
               INTO WS-OUT-2
           END-STRING
           DISPLAY "By SPACE:      [" WS-OUT-1 "]"
           DISPLAY "By two spaces: [" WS-OUT-2 "]"
           STOP RUN.
By SPACE:      [San, CA                       ]
By two spaces: [San Jose, CA                  ]

In the first STRING, one DELIMITED BY SIZE phrase applies to both ", " and WS-STATE, so the state is copied with its padding.

POINTER and OVERFLOW

WITH POINTER tells STRING where to start writing and leaves the pointer just after the last character written, so you can build text in several steps. ON OVERFLOW runs when the target is full:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. STRING-POINTER.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-LINE            PIC X(16)   VALUE SPACES.
       01  WS-PTR             PIC 99      VALUE 1.
       PROCEDURE DIVISION.
           STRING "ID=" "42" DELIMITED BY SIZE
               INTO WS-LINE WITH POINTER WS-PTR
           END-STRING
           DISPLAY "After step 1 pointer = " WS-PTR
           STRING ";NAME=" "Alexandria" DELIMITED BY SIZE
               INTO WS-LINE WITH POINTER WS-PTR
               ON OVERFLOW DISPLAY "Overflow: text was cut"
           END-STRING
           DISPLAY "[" WS-LINE "]"
           STOP RUN.
After step 1 pointer = 06
Overflow: text was cut
[ID=42;NAME=Alexa]

UNSTRING

UNSTRING splits a source field at delimiters into several receiving fields. It is how COBOL reads CSV lines and other delimited data:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. UNSTRING-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-CSV             PIC X(40)
           VALUE "1007,Ingrid Berg,Stockholm,2450.75".
       01  WS-ID              PIC X(6).
       01  WS-NAME            PIC X(15).
       01  WS-CITY            PIC X(12).
       01  WS-AMOUNT-TEXT     PIC X(10).
       01  WS-FIELDS          PIC 99      VALUE 0.
       PROCEDURE DIVISION.
           UNSTRING WS-CSV DELIMITED BY ","
               INTO WS-ID WS-NAME WS-CITY WS-AMOUNT-TEXT
               TALLYING IN WS-FIELDS
           END-UNSTRING
           DISPLAY "Id:     " WS-ID
           DISPLAY "Name:   " WS-NAME
           DISPLAY "City:   " WS-CITY
           DISPLAY "Amount: " FUNCTION NUMVAL(WS-AMOUNT-TEXT)
           DISPLAY "Fields: " WS-FIELDS
           STOP RUN.
Id:     1007
Name:   Ingrid Berg
City:   Stockholm
Amount: 0002450.75
Fields: 04

TALLYING IN counts how many receiving fields were filled. Each receiver is filled like a MOVE, so text is left-aligned and padded. FUNCTION NUMVAL converts the amount text to a number.

Several delimiters, COUNT and DELIMITER

DELIMITED BY ALL SPACE treats a run of spaces as one delimiter, and OR adds alternative delimiters. For each receiver you can capture the delimiter that ended it and the number of characters copied:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. UNSTRING-MORE.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-INPUT           PIC X(30)
           VALUE "red;green   blue/yellow".
       01  WS-WORD            PIC X(8)    OCCURS 4 TIMES.
       01  WS-DELIM           PIC X(3)    OCCURS 4 TIMES.
       01  WS-LEN             PIC 99      OCCURS 4 TIMES.
       01  WS-I               PIC 9.
       PROCEDURE DIVISION.
           UNSTRING WS-INPUT DELIMITED BY ";" OR "/" OR ALL SPACE
               INTO WS-WORD (1) DELIMITER IN WS-DELIM (1)
                                COUNT IN WS-LEN (1)
                    WS-WORD (2) DELIMITER IN WS-DELIM (2)
                                COUNT IN WS-LEN (2)
                    WS-WORD (3) DELIMITER IN WS-DELIM (3)
                                COUNT IN WS-LEN (3)
                    WS-WORD (4) DELIMITER IN WS-DELIM (4)
                                COUNT IN WS-LEN (4)
           END-UNSTRING
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 4
               DISPLAY WS-WORD (WS-I) " len " WS-LEN (WS-I)
                       " ended by [" WS-DELIM (WS-I) "]"
           END-PERFORM
           STOP RUN.
red      len 03 ended by [;  ]
green    len 05 ended by [   ]
blue     len 04 ended by [/  ]
yellow   len 06 ended by [   ]

green and yellow were ended by a run of spaces (yellow by the trailing padding of WS-INPUT), so their DELIMITER IN fields hold spaces. The ; and / delimiters are stored left-aligned and padded to 3 characters, like a MOVE.

Test yourself

What does DELIMITED BY SIZE mean in STRING?

  1. Stop at the first space
  2. Copy the whole source item
  3. Copy only the first character
  4. Use the target's size as a delimiter

Why should you usually MOVE SPACES to the target before STRING?

  1. STRING requires it syntactically
  2. STRING does not clear the target, so old characters can remain after the new text
  3. To set the pointer
  4. To avoid an overflow

What does TALLYING IN count in UNSTRING?

  1. The delimiters found
  2. The number of receiving fields that were filled
  3. The characters in the source
  4. The spaces