COBOL String

COBOL Examine COBOL Unstring Statement

Advertisements


The STRING statement concatenates the partial or complete contents of two or more data items or literals into one single data item.

STRING statement does the task of several MOVE statements job.

STRING used to concatenate one or more data items to a single data item.


Syntax:

STRING  {identifier-1/literal-1} [DELIMITED BY SIZE/SPACE/specified delimiter] 
	  {identifier-2/literal-2} [DELIMITED BY SIZE/SPACE/ specified delimiter] 
		.
		.
		{identifier-N/literal-N}
	INTO  	identifier
	[WITH POINTER ws-pointer]
	[ON OVERFLOW imperative-statements-1]
	[NOT ON OVERFLOW imperative-statements-2]
[END-STRING.]

STRING requires minimum two identifiers/literals are required concatenate.

STRING statement can concatenate alphabetic and alpha-numeric items.

STRING statement can’t do concatenation on numeric and floating point items.

STRING allows only SPACE which is figurative constants.

STRING will remove multiple instances of delimiter character.

STRING will consider all multiple spaces as a single space.

DELIMITED BY, WITH POINTER, ON OVERFLOW and NOT ON OVERFLOW clause is optional in STRING usage.

END-STRING is optional during the STRING statement usage.

END-STRING used as a scope terminator for STRING statement.


DELIMITED BY:

DELIMITED BY phase used to set the limit of the string during the concatenation.

DELIMITED BY phase is optional in STRING statement.

There are three ways of delimitation


1. SIZE:

DELIMITED BY SIZE used in STRING concatenation.

The string will concatenate with the same size of the input identifiers/literals.

Entire source string will be moved to the destination field.

No additional characters added to destination field.


2. SPACE:

DELIMITED BY SPACE used in STRING concatenation.

The string will concatenate with the data up to the first space found in the identifier.

Entire source string will be moved to the destination field.

SPACE will be added to destination field between every input identifier.


3. Specified delimiter:

DELIMITED BY special delimiter used in STRING concatenation.

The string will concatenate with the data up to the specified delimiter found in the identifier.

Entire source string will be moved to the destination field.

No additional characters added to destination field.


WITH POINTER:

POINTER option is used to points the first position of the destination data item.

POINTER option is optional in STRING statement.

If we specify the POINTER option, then we need to provide a value which is greater than ZERO or less than the length of data item.


ON OVERFLOW:

ON OVERFLOW is used to execute a set of imperative statements when overflow occurs during STRING concatenation.

ON OVERFLOW will trigger the imperative statements based on the implicit or explicit pointer value.

ON OVERFLOW triggers when the size of the concatenated strings is greater than the receiving field.


NOT ON OVERFLOW:

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.


Practical Example:


Below program shows the STRING coding in COBOL program.


Code:


   IDENTIFICATION DIVISION.                                        
   PROGRAM-ID. STRFUNC.                                            
   ENVIRONMENT DIVISION.                                           
   DATA DIVISION.                                                  
   WORKING-STORAGE SECTION.                                        
   01 WS-DATA		PIC X(20) VALUE ‘MAINFRAMESTECHHELP’.  
   01 WS-DATA1 	 	PIC X(40)                              
                                VALUE 'IS A MAINFRAME COMMUNITY'.  
   01 WS-OUTPUT-DATA        PIC X(70).                             
   PROCEDURE DIVISION.                                             
       DISPLAY 'STRING FUNCTION DISPLAY....'.                      
       STRING WS-DATA, WS-DATA1 DELIMITED BY SPACE                
         INTO WS-OUTPUT-DATA                                       
         ON OVERFLOW DISPLAY "ERROR OCCURED"                       
         NOT ON OVERFLOW DISPLAY "NO ERROR"                        
       END-STRING.                                                 
       DISPLAY 'DATA AFTER STRING FUNCTION : ' WS-OUTPUT-DATA.    
       STOP RUN.                                                  
           


COBOL Examine COBOL Unstring Statement

Advertisements