Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL STRING Statement

Intermediate 🕑 10 min read 👁 0 views

COBOL STRING Statement

The STRING statement concatenates multiple strings into one.

Syntax

STRING identifier-1 [DELIMITED BY identifier/literal/SIZE]
       identifier-2 [DELIMITED BY ...]
       ...
       INTO identifier-3
       [WITH POINTER identifier-4]
       [ON OVERFLOW imperative-statement]
[END-STRING]

Example Program

       IDENTIFICATION DIVISION.
       PROGRAM-ID. STRING-DEMO.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-FIRST-NAME  PIC X(10) VALUE 'John'.
       01 WS-LAST-NAME   PIC X(15) VALUE 'Smith'.
       01 WS-FULL-NAME   PIC X(30) VALUE SPACES.
       01 WS-CITY        PIC X(15) VALUE 'New York'.
       01 WS-STATE       PIC X(2) VALUE 'NY'.
       01 WS-ZIP         PIC X(5) VALUE '10001'.
       01 WS-ADDRESS     PIC X(30) VALUE SPACES.
       01 WS-PTR         PIC 99 VALUE 1.

       PROCEDURE DIVISION.
      * Simple concatenation
           STRING WS-FIRST-NAME DELIMITED BY SPACE
                  ' ' DELIMITED BY SIZE
                  WS-LAST-NAME DELIMITED BY SPACE
                  INTO WS-FULL-NAME
           DISPLAY 'Full Name: [' WS-FULL-NAME ']'

      * Using pointer
           MOVE 1 TO WS-PTR
           MOVE SPACES TO WS-ADDRESS
           STRING WS-CITY DELIMITED BY '  '
                  ', ' DELIMITED BY SIZE
                  WS-STATE DELIMITED BY SIZE
                  ' ' DELIMITED BY SIZE
                  WS-ZIP DELIMITED BY SIZE
                  INTO WS-ADDRESS
                  WITH POINTER WS-PTR
           DISPLAY 'Address: [' WS-ADDRESS ']'
           DISPLAY 'Pointer at: ' WS-PTR

      * Overflow handling
           MOVE SPACES TO WS-FULL-NAME
           STRING 'This is a very long string'
                  ' that will overflow'
                  DELIMITED BY SIZE
                  INTO WS-FULL-NAME
                  ON OVERFLOW
                      DISPLAY 'String overflow occurred'
           END-STRING

           STOP RUN.

Expected Output

Full Name: [John Smith                    ]
Address: [New York, NY 10001             ]
Pointer at: 22
String overflow occurred

Key Points

  • DELIMITED BY controls what to include
  • SIZE means include entire field
  • POINTER tracks position in target
  • ON OVERFLOW handles truncation