UNSTRING Statement


The UNSTRING statement takes a single string, breaks it down into several separate strings, and places them into the variables. It does the task of several MOVE statements and separates a single string into multiple based on the delimiter supplied.

UNSTRING requires a minimum of two receiving variables. It is applicable only to alphabetic and alpha-numeric items and not applicable to numeric and floating-point items.

Syntax -

---+----2----+----3----+----4----+----5
UNSTRING source-string 
       [DELIMITED BY delimiter1] 
       INTO target-string-1 [target-string-2 ...]
       [WITH POINTER pointer-name]
       [TALLYING IN counter-name]
       [ON OVERFLOW statements-block-1]
   [NOT ON OVERFLOW statements-block-2]
[END-UNSTRING].
Note! All statements coded in [ ] are optional.

Parameters -

  • source-string - This is the string that we want to break down.
  • DELIMITED BY delimiter1 - Specifies the delimiter1 used to specify where to split the string. If it's not coded, then each character is considered separately.
  • INTO - Specifies the variables where the divided strings of the source string should be placed.
  • WITH POINTER - It will hold the position of next delimiter in the source string immediately after the last character that was processed.
  • TALLYING IN - Used to count the number of characters that have been transferred to the target fields.
  • 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-UNSTRING - This is the explicit scope terminator for the UNSTRING statement.

Practical Example -


Scenario - Split the string using UNSTRING statement.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. UNSTREXP.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-INPUT       PIC X(70) VALUE
             'MAINFRAMESTECHHELP,IS A MAINFRAME COMMUNITY'.
          05 WS-OUTPUT1     PIC X(30).
          05 WS-OUTPUT2     PIC X(40).

       PROCEDURE DIVISION.
           UNSTRING WS-INPUT DELIMITED BY ","
               INTO WS-OUTPUT1, WS-OUTPUT2
                    ON OVERFLOW DISPLAY "ERROR OCCURED"
                NOT ON OVERFLOW
                        DISPLAY "WS-OUTPUT1:  ", WS-OUTPUT1 
                        DISPLAY "WS-OUTPUT2:  ", WS-OUTPUT2 
           END-UNSTRING.

           STOP RUN.

Output -

WS-OUTPUT1:  MAINFRAMESTECHHELP
WS-OUTPUT2:  IS A MAINFRAME COMMUNITY