TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - String Statement


The STRING statement strings together the partial or complete contents of two or more data items or literals into one single data item. One STRING statement can be written instead of a series of MOVE statements.

The STRING statement is used to concatenate non-numeric items together. Any number of items can be concatenated. Entire or partial strings may be concatenated. To use the entire string, delimit it by size. To use only a portion of a string, delimit it by whatever character indicates the end of the data you want to concatenate.

Syntax:

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

identifier-1, literal-1 - Represents the sending fields.

DELIMITED BY - Sets the limits of the string. A delimiter for each set of sending fields that, if encountered, causes those sending fields to stop being transferred (DELIMITED BY phrase)
  • identifier-2 , literal-2 - It is used as delimiters; that is, characters that delimit the data to be transferred.
  • SIZE - Transfers the complete sending area.
INTO - Identifies the receiving field(i.e. identifier-3 - Represents the receiving field).

POINTER - (Optional) An integer data item that indicates the leftmost character position within the receiving field into which data should be transferred (WITH POINTER phrase).

identifier-4 - Represents the pointer field. identifier-4 must be large enough to contain a value equal to the length of the receiving field plus 1.

ON OVERFLOW - (Optional) Action to be taken if the receiving field is filled before all of the sending data has been processed (ON OVERFLOW phrase).

END-STRING - This explicit scope terminator serves to delimit the scope of the STRING statement.
STRING Rules:
  • Receiving fields must not be:
    • Edited field
    • With Justified clause
    • With reference modification

  • Pointer variable must be:
    • Elementary and Numeric
    • Large enough to hold max length of receiving field

  • ON OVERFLOW executed when pointer Value <= or exceeds max length of receiving field

Example 1:

When we execute the below STRING statement, the results obtained will be like those illustrated in the figure after the statement.

STRING ID-1 ID-2 DELIMITED BY ID-3
            ID-4 ID-5 DELIMITED BY SIZE
  INTO ID-7 WITH POINTER ID-8
END-STRING

image missing

Example 2:

........
FILE SECTION.
01  RCD-01.
    05  CUST-INFO.
        10  CUST-NAME    PIC X(15).
        10  CUST-ADDR    PIC X(35).
    05  BILL-INFO.
        10  INV-NO       PIC X(6).
        10  INV-AMT      PIC $$,$$$.99.
        10  AMT-PAID     PIC $$,$$$.99.
        10  DATE-PAID    PIC X(8).
        10  BAL-DUE      PIC $$,$$$.99.
        10  DATE-DUE     PIC X(8).
........
WORKING-STORAGE SECTION.
77  RPT-LINE             PIC X(120).
77  LINE-POS             PIC S9(3).
77  LINE-NO              PIC 9(5) VALUE 1.
77  DEC-POINT            PIC X VALUE ".".
-----
PROCEDURE DIVISION.
    .......
    MOVE SPACES TO RPT-LINE.
    MOVE 4      TO LINE-POS.

    STRING
          LINE-NO SPACE CUST-INFO INV-NO SPACE DATE-DUE SPACE
      DELIMITED BY SIZE
          BAL-DUE
      DELIMITED BY DEC-POINT
      INTO RPT-LINE
      WITH POINTER LINE-POS
    END-STRING.
    -------
    STOP RUN.

STRING results:

Let us consider the record RCD-01 contains the following information (the symbol b indicates a blank space):

J.B.bSMITHbbbbb
444bSPRINGbST.,bCHICAGO,bILL.bbbbbb
A14275
$4,736.85
$2,400.00
09/22/76
$2,336.85
10/22/76

When the STRING statement is performed, items are moved into RPT-LINE as shown in the table below.

ItemPositionsData
LINE-NO4 - 800001
Space9b
CUST-INFO10 - 59J.B.bSMITHbbbbb444bSPRINGbST.,bCHICAGO,bILL.bbbbbb
INV-NO60 - 65A14275
Space66b
DATE-DUE67 - 7410/22/76
Space75b
Portion of BAL-DUE that precedes the decimal point76 - 81$2,336

After the STRING statement is performed, the value of LINE-POS is 82, and RPT-LINE has the values,

column 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
value     00001 J.B. SMITH     444 SPRING ST., CHICAGO, ILL.      A14275 10/22/76 $2,336


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal