Exemple – STRING

Programme utilisant le STRING:

EXEMPLE 1

IDENTIFICATION DIVISION.
PROGRAM-ID.  STRING.
AUTHOR.      LA COMMUNAUTE DU COBOL.

ENVIRONMENT DIVISION.                                            
CONFIGURATION SECTION.                                           
SPECIAL-NAMES.                                                   
    DECIMAL-POINT IS COMMA. 

DATA DIVISION.
WORKING-STORAGE SECTION.

01  WS-AUXILIARES.
    05  WS-DEL           PIC X(001)     VALUE '&'. 
    05  PT1              PIC 9(005)     VALUE 1. 
    05  CHAMP-1          PIC X(006)     VALUE 'AB*CD'. 
    05  CHAMP-2          PIC X(006)     VALUE 'JKL*MN'. 
    05  CHAMP-3          PIC X(006)     VALUE 'S*TUV'. 
    05  CAMPO-123        PIC X(020)     VALUE SPACES.
    05  WS-NOM-01        PIC X(020)     VALUE 'La'.
    05  WS-NOM-02        PIC X(020)     VALUE 'Communaute'.
    05  WS-NOM-03        PIC X(020)     VALUE 'Du'.
    05  WS-NOM-04        PIC X(020)     VALUE 'Cobol'.
    05  WS-NOM-COMPLET   PIC X(080)     VALUE SPACES.

PROCEDURE DIVISION.

001-INIT.

    STRING                                                    
          WS-NOM-01 DELIMITED BY SPACES ' ' DELIMITED BY SIZE 
          WS-NOM-02 DELIMITED BY SPACES ' ' DELIMITED BY SIZE 
          WS-NOM-03 DELIMITED BY SPACES ' ' DELIMITED BY SIZE 
          WS-NOM-04                       
      INTO  
          WS-NOM-COMPLET                                     
    END-STRING
    DISPLAY 'Nom complet: ' WS-NOM-COMPLET

* Resultat du test:

* Nom complet: La Communaute Du Cobol

    STRING CHAMP-1 CHAMP-2 CHAMP-3 
           DELIMITED BY "*" 
      INTO CHAMP-123
    END-STRING
    DISPLAY 'CHAMP-123: ' CHAMP-123

* Resultat du test:

* CHAMP-123: ABJKLS         

    STRING CHAMP-1 CHAMP-2 CHAMP-3 
           DELIMITED BY "*" 
      INTO CHAMP-123 
           POINTER PT1
    END-STRING
    DISPLAY 'CHAMP-123: ' CHAMP-123
    DISPLAY 'PT1      : ' PT1

* Resultat du test:

* CHAMP-123: ABJKLS              
* PT1      : 00007

    STRING CHAMP-1 CHAMP-2 CHAMP-3 
           DELIMITED BY "*" 
      INTO CHAMP-123 
           POINTER PT1
    END-STRING
    DISPLAY 'CHAMP-123: ' CHAMP-123
    DISPLAY 'PT1      : ' PT1

* Resultat du test:

* CHAMP-123: ABJKLSABJKLS                      
* PT1      : 00013

    MOVE SPACES   TO CHAMP-123 
    MOVE 'AB&XYZ' TO CHAMP-1
    MOVE 'LMN&OP' TO CHAMP-2
    MOVE 'ST&V  ' TO CHAMP-3

    STRING CHAMP-1 CHAMP-2 CHAMP-3 
           DELIMITED BY WS-DEL 
      INTO CHAMP-123
    END-STRING
    DISPLAY 'CHAMP-123: ' CHAMP-123

* Resultat du test:
* CHAMP-123: ABLMNST             

    STOP RUN.

EXEMPLE 2

IDENTIFICATION DIVISION.
PROGRAM-ID. STRING.
AUTHOR.     LA COMMUNAUTE DU COBOL.

DATA DIVISION.
FILE SECTION.

WORKING-STORAGE SECTION.

01          WS-DATE-HEURE.
    05      WS-FUNCTION-CURRENT-DATE PIC X(14).

01          WS-DATE-HEURE-FORMAT.
    05      WS-DATE-FORMAT           PIC X(10).
    05      WS-HEURE-FORMAT          PIC X(08).

PROCEDURE DIVISION.

    MOVE FUNCTION CURRENT-DATE TO WS-FUNCTION-CURRENT-DATE
    DISPLAY "WS-DATE-HEURE - " WS-FUNCTION-CURRENT-DATE
	
    STRING
           WS-FUNCTION-CURRENT-DATE (07:2) "/"
           WS-FUNCTION-CURRENT-DATE (05:2) "/"
           WS-FUNCTION-CURRENT-DATE (01:4)
      INTO WS-DATE-FORMAT	
    END-STRING
	     
    STRING
           WS-FUNCTION-CURRENT-DATE (09:2) ":"
           WS-FUNCTION-CURRENT-DATE (11:2) ":"
           WS-FUNCTION-CURRENT-DATE (13:2)
      INTO WS-HORA-FORMATADA
    END-STRING

    DISPLAY "WS-DATE-FORMAT  : " WS-DATE-FORMAT
    DISPLAY "WS-HEURE-FORMAT : " WS-HEURE-FORMAT

    STOP RUN.
Resultat:
WS-DATE-HEURE - 20220302011708
WS-DATE-FORMAT  : 02/03/2022
WS-HEURE-FORMAT : 01:17:08