STRING statement strings together the partial or complete contents of two or more data items or literals into one single data item. STRING needs at least two identifiers or literals. It can only concatenate alphabetic and alpha-numeric items. It will remove multiple instances of delimiter character and consider multiple spaces as a single space. END-STRING, DELIMITED BY, WITH POINTER, ON OVERFLOW and NOT ON OVERFLOW clause is optional in STRING usage.
STRING Statement Syntax
STING {Variable-1} [DELIMITED BY SIZE/SPACE/specified delimiter] {Variable-2} [DELIMITED BY SIZE/SPACE/ specified delimiter] …….. …….. INTO identifier [WITH POINTER ws-pointer] [ON OVERFLOW imperative-statements-1] [NOT ON OVERFLOW imperative-statements-2] [END-STRING.]
DELIMITED BY phrase: Sets the limits of the string & its optional.
- SIZE – The string will concatenate with the same size of the input identifiers/literals.
- SPACE – String will concatenate with the data up to the first space found in the identifier. SPACE will be added to the destination field between every input identifier.
- Specified delimiter – String will concatenate with the data up to the specified delimiter found in the identifier.
INTO phrase: Identifies the receiving field.
POINTER phrase: Points to a character position in the receiving field. The pointer field indicates a relative alphanumeric character position, DBCS character position, or national character position when the receiving field is of usage DISPLAY, DISPLAY-1, or NATIONAL, respectively.
ON OVERFLOW phrases: ON OVERFLOW is used to execute a set of imperative statements when overflow occurs during STRING concatenation. This will be triggered when the size of the concatenated strings is greater than the receiving field.
NOT ON OVERFLOW phrases: NOT ON OVERFLOW is used to execute the set of imperative statements when the STRING concatenation is successful. NOT ON OVERFLOW is fully reverse to ON OVERFLOW functionality.
END-STRING phrase: This explicit scope terminator serves to delimit the scope of the STRING statement. END-STRING permits a conditional STRING statement to be nested in another conditional statement. END-STRING can also be used with an imperative STRING statement.
Example with STRING Statement
STRING FIRST-NAME DELIMITED BY SPACE ' ' DELIMITED BY SIZE LAST-NAME DELIMITED BY SPACE ' ' DELIMITED BY SIZE INITIAL DELIMITED BY SPACE INTO NAME-OUT ON OVERFLOW DISPLAY “ERROR FOUND” NOT ON OVERFLOW DISPLAY “SUCCESSFUL” END-STRING.
In the Data Division, the programmer has defined the following fields:
01 RPT-LINE PICTURE X(120).
01 LINE-POS PICTURE 99.
01 LINE-NO PICTURE 9(5) VALUE 1.
01 DEC-POINT PICTURE X VALUE ".".
In the File Section, he or she has defined the following input record:
01 RCD-IN.
05 CUST-INFO.
10 CUST-NAME PICTURE X(15).
10 CUST-ADDR PICTURE X(34).
05 BILL-INFO.
10 INV-NO PICTURE X(6).
10 INV-AMT PICTURE $$,$$$.99.
10 AMT-PAID PICTURE $$,$$$.99.
10 DATE-PAID PICTURE X(8).
10 BAL-DUE PICTURE $$,$$$.99.
10 DATE-DUE PICTURE X(8).
The programmer wants to construct an output line consisting of portions of the information from RCD-01. The line is to consist of a line number, customer name and address, invoice number, date due, and balance due, truncated to the dollar figure shown. The record as read in contains the following information:
STRING Input
J.B.bSMITHbbbbb 444bSPRINGbST.,bCHICAGO,bILL.bbbbb A14275 $4,736.85 $2,400.00 09/22/76 $2,336.85 10/22/76
In the Procedure Division, the programmer initializes RPT-LINE to SPACES and sets LINE-POS (which is to be used as the pointer field) to 4. Then he issues this
STRING LINE-NO SPACE CUST-INFO SPACE INV-NO SPACE DATE-DUE SPACE DELIMITED BY SIZE, BAL-DUE DELIMITED BY DEC-POINT INTO RPT-LINE WITH POINTER LINE-POS.
When the statement is executed, the following actions take place:
- The field LINE-NO is moved into positions 4 through 8 of RPT-LINE.
- Space is moved into position 9.
- The group item CUST-INFO is moved into positions 10 through 58.
- Space is moved into position 59.
- INV-NO is moved into positions 60 through 65.
- Space is moved into position 66.
- DATE-DUE is moved into positions 67 through 74.
- Space is moved into position 75.
- The portion of BAL-DUE that precedes the decimal point is moved into positions 76 through 81.
STRING Output
00001 J.B. SMITH 444 SPRING ST., CHICAGO, ILL. A14725 10/22/76 $2,336