STRING Statement


The STRING statement is used to concatenate the contents of two or more data item or literals into a single data item. It enables the construction of strings by combining separate pieces of data together.

One STRING statement does the task of a series of MOVE statements.

STRING requires a minimum of two variables or literals to concatenate. It is applicable only to alphabetic and alpha-numeric items and not applicable to numeric and floating-point items.

Syntax -

1----+----2----+----3----+----4----+----5----+
  STRING source-item-1 [DELIMITED BY delimiter-1]
      [source-item-2 DELIMITED BY delimiter-2] [...]
      INTO destination-item
      [WITH POINTER pointer-name]
      [ON OVERFLOW statements-block-1]
      [NOT ON OVERFLOW statement-block-2]
      [END-STRING].
Note! All statements coded in [ ] are optional.

Parameters -

  • source-item - These are the variables that we want to concatenate. We can string together multiple source items.
  • DELIMITED BY - This clause specifies the delimiter that specifies the end of each source item. If we use SIZE, it'll consider the whole variable.
  • destination-item - This is the result of the concatenation. It's where the stringed data will be stored.
  • WITH POINTER pointer-name - This optional clause maintains the position in the destination item. It can be set before the stringing process and can be checked afterward.
  • ON OVERFLOW statements-block-1 - Specifies the set of statements that are executed when ON OVERFLOW occurs.
  • NOT ON OVERFLOW statement-block-2 - Specifies the set of statements that are executed when the STRING operation is successful.
  • END-STRING - This is the explicit scope terminator for the STRING statement.

Practical Example -


Scenario - Concatenate two strings separated by space.

Code -

----+----1----+----2----+----3----+----4----+----5
       IDENTIFICATION DIVISION.
       PROGRAM-ID. STRINGE. 

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-INP1    PIC X(18)
		                VALUE 'MAINFRAMESTECHHELP'.
          05 WS-INP2    PIC X(40)
                        VALUE 'IS A MAINFRAME COMMUNITY'.
          05 WS-OUTPUT  PIC X(70).

       PROCEDURE DIVISION.

           STRING WS-INP1 DELIMITED BY SIZE, 
                  SPACE,
                  WS-INP2 DELIMITED BY SIZE
             INTO WS-OUTPUT
                  ON OVERFLOW DISPLAY "Error occured"
              NOT ON OVERFLOW DISPLAY "Result: " WS-OUTPUT
           END-STRING.
           STOP RUN.

Output -

Result: MAINFRAMESTECHHELP IS A MAINFRAME COMMUNITY

Explaining Example -

In the above example, WS-INP1 and WS-INP2 are concatenated by separating a space between them and storing them in the WS-OUTPUT variable. Because of no overflow, NOT ON OVERFLOW gets executed, and the result is displayed.