STRING Statement
The STRING statement concatenates separate literals and data items into a single data string.
General Format:
STRING { {src-string-1} ... [DELIMITED BY {delimiter-1}] } ...
{SIZE }
INTO target-string-1
[ WITH POINTER pointer-variable-1 ]
[ ON OVERFLOW statement-1 ]
[ NOT ON OVERFLOW statement-2 ]
[ END-STRING ]
Syntax:
Src-string-nis a alphanumeric data element, literal, or data returned from a function call.pointer-variable-nis a numeric data element with a positive integer value.delimiter-nis a character string.target-string-1is an alphanumeric data item.statement-nis an imperative statement.
General Rules:
- The
STRINGstatement concatenates consecutivesrc-string-n, and stores the result in thetarge-stringdata item that follows theINTOphrase. - There is no limit to the number of
src-string-nidentifiers that may be listed. - The
DELIMITED BYclause provides a way to identify a substring of asrc-identifier, for purposes of theSTRINGoperation. - The
DELIMITED BY delimiter-1clause causes theSTRINGoperation to stop copying data from thesrc-stringwhen thedelimiter-1is encountered in thesrc-string.Delimiter-1is not copied to the target string. - The
DELIMITED BY SIZEclause causes the entiresrc-stringto be copied into the target string. - If there is no
DELIMITED BYphrase, thenDELIMITED BY SIZEis assumed. - The
WITH POINTERclause provides a way to set a position in thetarget-stringwhere theSTRINGstatement will copy the selected source using apointer-variable. - The
WITH POINTERclause causes an explicitpointer-variableto be referenced by theSTRINGoperation when it begins to copy data from asrc-stringto atarget-string. The value of thepointer-variableis used as the position in thetarget-stringat which to copy the data. - When the
WITH POINTERclause is used,pointer-variable-nmust be set to a positive integer value before theSTRINGstatement is executed. Most commonly, thepointer-variableis initialized to 1, indicating that theSTRINGstatement should begin copying into thetarget-stringat the first byte of thetarget-string. - The
pointer-variableis automatically incremented by 1 for each character that is copied into thetarget-string. - If there is no
WITH POINTERclause, the position at which thesrc-stringis copied into thetarget-stringis the next character position after the last character position occupied by the previoussrc-string. - The
ON OVERFLOWcondition is triggered when thepointer-variabledoes not contain a positive value, or when the length oftarget-stringis less than the combined length of thesrc-stringsnamed in theSTRINGstatement. - When an
ON OVERFLOWcondition exists, theSTRINGis terminated, and theON OVERFLOW statement-1is executed. - The
NOT ON OVERFLOWcondition exists when theSTRINGstatement has completed, an theON OVERFLOWcondition does not exist. - When a
NOT ON OVERFLOWcondition exists,statement-2is executed.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. STRING-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ADDRESS-1
PIC X(40) VALUE"231/RUE SAINT-HONORE/75001 PARIS/FRANCE".
01 ADDRESS-2.
05 ADDRESS-NUM PIC X(5) VALUE SPACES.
05 ADDRESS-NAME PIC X(20) VALUE SPACES.
05 ADDRESS-CITY PIC X(20) VALUE SPACES.
05 ADDRESS-COUNTRY PIC X(20) VALUE SPACES.
01 ADDRESS-3 PIC X(60) VALUE SPACES.
01 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
UNSTRING ADDRESS-1 DELIMITED BY "/"
INTO ADDRESS-NUM
ADDRESS-NAME
ADDRESS-CITY
ADDRESS-COUNTRY.
DISPLAY "THE STREET #: " ADDRESS-NUM LINE 4 COL 10.
DISPLAY "THE STREET NAME: " ADDRESS-NAME LINE 5 COL 10.
DISPLAY "THE CITY: " ADDRESS-CITY LINE 6 COL 10.
DISPLAY "THE COUNTRY: " ADDRESS-COUNTRY LINE 7 COL 10.
ACCEPT DUMMY LINE 7 COL 33.
STRING ADDRESS-NUM DELIMITED BY " "
" " DELIMITED BY SIZE
ADDRESS-NAME DELIMITED BY " "
", " DELIMITED BY SIZE
ADDRESS-CITY DELIMITED BY " "
", " DELIMITED BY SIZE
ADDRESS-COUNTRY DELIMITED BY SIZE INTO ADDRESS-3
ON OVERFLOW
DISPLAY "STRING FAILED ON OVERFLOW" LINE 10 COL 10
NOT ON OVERFLOW DISPLAY "THE ADDRESS IS " ADDRESS-3 LINE 10 COL 10
END-STRING.
DISPLAY "STRING-1 FINISHED!" LINE 12 COL 10.
ACCEPT DUMMY LINE 12 COL 30.
STOP RUN.