Thursday, 1 September 2016

How to remove spaces in a String

How to remove front and back spaces in a variable and write it in middle of two variable,

01  VAR-SPACE            PIC X(10) VALUE '   DATA   '.
01  LEAD-SPACE         PIC 9(04)  COMP.
01  TRAIL-SPACE        PIC 9(04)  COMP.
01  LENG-SPACE         PIC 9(04)  COMP.
01  FIRST-VAR             PIC X(07) VALUE 'VALID -'.
01  TRAIL-VAR            PIC X(10) VALUE '- REQUIRED'.
01  FINAL-VAR            PIC X(21) VALUE SPACES.


INSPECT              VAR-SPACE
      TALLYING     LEAD-SPACE          
      FOR LEADING SPACES    
                 
INSPECT    FUNCTION REVERSE(VAR-SPACE)
     TALLYING    TRAIL-SPACE          
     FOR LEADING SPACES      
                
COMPUTE  SPACE-LENG =                        
                  LENGTH OF  VAR-SPACE - LEAD-SPACE - TRAIL-SPACE     
          
ADD    1                    TO   LEAD-SPACE    

STRING FIRST-VAR DELIMITED BY SIZE  
       VAR-SPACE (LEAD-SPACE:SPACE-LENG)
                                DELIMITED BY SIZE
       TRAIL-VAR   DELIMITED BY SIZE  
       INTO FINAL-VAR.    

DISPLAY "The result will be: " FINAL-VAR.


The result will be: VALID -DATA- REQUIRED.

Here the spaces which are present in front and back of 'VAR-SPACE' variable has to be removed.
So we have used INSPECT function to check for number of spaces in front and back using TALLYING function.
Then using reference modification only the DATA is moved after removing the leading and trailing spaces.

No comments:

Post a Comment

Expense Handler Application with advance technologies

Budget Planner  One of the application developed with Ionic 4 and Python-Flask.  Ionic 4 code:  https://github.com/logeshbuiltin/Expense...