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-n
is a alphanumeric data element, literal, or data returned from a function call.pointer-variable-n
is a numeric data element with a positive integer value.delimiter-n
is a character string.target-string-1
is an alphanumeric data item.statement-n
is an imperative statement.
General Rules:
- The
STRING
statement concatenates consecutivesrc-string-n
, and stores the result in thetarge-string
data item that follows theINTO
phrase. - There is no limit to the number of
src-string-n
identifiers that may be listed. - The
DELIMITED BY
clause provides a way to identify a substring of asrc-identifier
, for purposes of theSTRING
operation. - The
DELIMITED BY delimiter-1
clause causes theSTRING
operation to stop copying data from thesrc-string
when thedelimiter-1
is encountered in thesrc-string
.Delimiter-1
is not copied to the target string. - The
DELIMITED BY SIZE
clause causes the entiresrc-string
to be copied into the target string. - If there is no
DELIMITED BY
phrase, thenDELIMITED BY SIZE
is assumed. - The
WITH POINTER
clause provides a way to set a position in thetarget-string
where theSTRING
statement will copy the selected source using apointer-variable
. - The
WITH POINTER
clause causes an explicitpointer-variable
to be referenced by theSTRING
operation when it begins to copy data from asrc-string
to atarget-string
. The value of thepointer-variable
is used as the position in thetarget-string
at which to copy the data. - When the
WITH POINTER
clause is used,pointer-variable-n
must be set to a positive integer value before theSTRING
statement is executed. Most commonly, thepointer-variable
is initialized to 1, indicating that theSTRING
statement should begin copying into thetarget-string
at the first byte of thetarget-string
. - The
pointer-variable
is automatically incremented by 1 for each character that is copied into thetarget-string
. - If there is no
WITH POINTER
clause, the position at which thesrc-string
is copied into thetarget-string
is the next character position after the last character position occupied by the previoussrc-string
. - The
ON OVERFLOW
condition is triggered when thepointer-variable
does not contain a positive value, or when the length oftarget-string
is less than the combined length of thesrc-strings
named in theSTRING
statement. - When an
ON OVERFLOW
condition exists, theSTRING
is terminated, and theON OVERFLOW statement-1
is executed. - The
NOT ON OVERFLOW
condition exists when theSTRING
statement has completed, an theON OVERFLOW
condition does not exist. - When a
NOT ON OVERFLOW
condition exists,statement-2
is 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.