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.
STRING identifier-1 or literal-1
DELIMITED BY identifier-2 or literal-2 or SIZE
[WITH POINTER identifier-4]
[ON OVERFLOW imperative-statement-1]
[NOT ON OVERFLOW imperative-statement-2]
- Represents the sending fields.
- 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.
- Identifies the receiving field(i.e. identifier-3
- Represents the receiving field).
- (Optional) An integer data item that indicates the leftmost character position within the receiving field into which data should be transferred (WITH POINTER phrase).
- Represents the pointer field. identifier-4 must be large enough to contain a value equal to the length of the receiving field plus 1.
- (Optional) Action to be taken if the receiving field is filled before all of the sending data has been processed (ON OVERFLOW phrase).
- This explicit scope terminator serves to delimit the scope of the STRING statement.
- 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
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
10 CUST-NAME PIC X(15).
10 CUST-ADDR PIC X(35).
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).
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 ".".
MOVE SPACES TO RPT-LINE.
MOVE 4 TO LINE-POS.
LINE-NO SPACE CUST-INFO INV-NO SPACE DATE-DUE SPACE
DELIMITED BY SIZE
DELIMITED BY DEC-POINT
WITH POINTER LINE-POS
Let us consider the record RCD-01 contains the following information (the symbol b indicates a blank space):
When the STRING statement is performed, items are moved into RPT-LINE as shown in the table below.
|LINE-NO||4 - 8||00001|
|CUST-INFO||10 - 59||444bSPRINGbST.,bCHICAGO,bILL.bbbbbb|
|INV-NO||60 - 65||A14275|
|DATE-DUE||67 - 74||09/22/76|
|Portion of BAL-DUE that precedes the decimal point||76 - 81||$2,336.85|
After the STRING statement is performed, the value of LINE-POS is 82, and RPT-LINE has the values,
value 00001 J.B. SMITH 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!