String and Unstring

Today we are are gonna discuss the String and Unstring statements of Cobol. These statements are used for concatenate or parse a string/or characters. These statements are utilizing Identification division to get the desired results. these statements will remove the multiple instances of delimiter character.All the embedded spaces will be treated as a single space. A new field or a string is created with the leading spaces removed and all multiple embedded spaces reduced to a single space. The length of the actual text within the output field is also provided.

Unstring.

Is used to split the given string into a single or multiple data names, with a delimiter character( lets say space) .

Syntax: unstring <input_data_name>  delimited by space  into <data_name_1> <data_name_2>

e.g. UNSTRING WS-FULL-NAME       DELIMITED BY SPACE  INTO WS-FIRST-NAME WS-LAST-NAME.

String.

String used to concatenate to or more data names.

Syntax: string <dataname_i> delimited by space <dataname_2> into <output_data-name>

e.g. STRING WS-FIRST-NAME   DELIMITED BY SPACE  WS-LAST-NAME DELIMITED BY SPACE INTO WS-FULL-NAME.

Code:

Sample Program:

IDENTIFICATION DIVISION.                                        
PROGRAM-ID. VSTRING.                                            
AUTHOR    . SHIBU.T.                                            
*                                                                
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
77  WS-FULL-NAME                PIC X(30).                      
77  WS-FIRST-NAME               PIC X(05).                      
77  WS-LAST-NAME                PIC X(15).                      
*                                                                
EJECT.                                                          
PROCEDURE DIVISION.                                             
A00100-MAIN-LINE.                                               
     DISPLAY ‘***STRING***’.                                     
     MOVE ‘SHIBU’                TO WS-FIRST-NAME.               
     MOVE ‘THANNIKKUNNATH’       TO WS-LAST-NAME.                
     STRING WS-FIRST-NAME        DELIMITED BY SPACE              
            ‘ ‘                  DELIMITED BY SIZE               
            WS-LAST-NAME         DELIMITED BY SPACE              
                                 INTO WS-FULL-NAME.              
     DISPLAY ‘FIRST NAME: ‘ WS-FIRST-NAME.                       
     DISPLAY ‘LAST  NAME: ‘ WS-LAST-NAME.                        
     DISPLAY ‘FULL  NAME: ‘ WS-FULL-NAME.                        
     DISPLAY ‘**UNSTRING***’.                                    
     MOVE SPACES TO WS-FIRST-NAME, WS-LAST-NAME.                
 
     UNSTRING WS-FULL-NAME       DELIMITED BY SPACE              
                                 INTO WS-FIRST-NAME WS-LAST-NAME.
     DISPLAY ‘FULL  NAME: ‘ WS-FULL-NAME.                        
     DISPLAY ‘FIRST NAME: ‘ WS-FIRST-NAME.                       
     DISPLAY ‘LAST  NAME: ‘ WS-LAST-NAME.                        
     EXIT.                                                       
     STOP RUN.                                                   

 

outputs:

image

Leave a Reply

Your email address will not be published. Required fields are marked *