MODULE 8: String handling
STRING
- STRING is used to join together one or more strings or partial source strings to form a single destination string
- This is also used to transfer characters from a string to another string starting at some particular character position either in the receiving field or in the sending fields
- Basic syntax:-
STRING {identifier-1/literal-1}, {identifier-2/literal-2}… DELIMITED BY {identifier-3/literal-3/SIZE} {identifier-4/literal-4}, {identifier-5/literal-5} DELIMITED BY {identifier-6/literal-6/SIZE} INTO identifier-7 [;ON OVERFLOW <imperative-statement>]
- Above syntax is used to concatenate two or more strings into one by putting them side by side
- Sending strings (identifier-1/literal-1, identifier-2, literal-2, identifier-3, literal-3 etc.) must be alphanumeric data item/literal/figurative constant. Figurative constant are treated as a single character
- identifier-7 is receiving string where concatenated string will be stored. If final concatenated string to be placed in identifier-7 is less in size, then remaining positions of identifier-7 will remain as it is
- DELIMITED BY phrase is mandatory to code
- DELIMITED BY SIZE moves entire content of sending field to receiving field
- DELIMINTED BY {identifier/literal} moves content of sending field to receiving field from left to tight until it encounters character {identifier/literal} in sending field
- ON OVERFLOW phrase is used to execute imperative statement when there is overflow situation. Overflow occurs when the final concatenated string is greater in size when compared to size of receiving field.
- Example 1:-
In DATA DIVISON,01 WS-D PIC XX VALUE ‘9 ‘. 01 WS-M PIC X(9) VALUE ‘JANUARY ’. 01 WS-Y PIC X(4) VALUE ‘1993’. 01 WS-DATE PIC X(15) VALUE ALL SPACES.In PROCEDURE DIVISION,STRING WS-D DELIMITED BY SPACE ‘-‘ DELIMITED BY SIZE WS-M DELIMITED BY SPACE ‘-‘ DELIMITED BY SIZE WS-Y DELIMITED BY SIZE INTO WS-DATEResult:- WS-DATE will contain ‘9-JANUARY-1999 ‘
- Example 2:-
In DATA DIVISON,01 WS-A PIC X(4) VALUE ‘1,134‘. 01 WS-B PIC X(6) VALUE ‘HI I AM’. 01 WS-C PIC X(6) VALUE ‘OK, NP’. 01 WS-D PIC X(10) VALUE ‘-’.In PROCEDURE DIVISION,STRING WS-A, WS-B, WS-C DELIMITED BY ‘,’, ‘ ‘ INTO WS-DResult:- WS-D will contain ‘1HIOK-----‘
- Example 3:-
In DATA DIVISON,01 WS-MSG PIC X(13) VALUE ‘123 IS NUMBER‘.In PROCEDURE DIVISION,STRING ‘789’ DELIMITED BY SIZE INTO WS-MSGResult:- WS-MSG will contain ‘789 IS NUMBER‘
- Example 4:-
In DATA DIVISON,01 WS-A PIC X(6) VALUE ‘I KNOW‘. 01 WS-B PIC X(9) VALUE ‘LOVE LIFE’. 01 WS-C PIC X(17) VALUE ‘MAINFRAME IS BEST’. 01 WS-D PIC X(16) VALUE ALL ‘*’.In PROCEDURE DIVISION,STRING WS-A, WS-B, WS-C DELIMITED BY SPACES INTO WS-DResult:- WS-D will contain ‘ILOVEMAINFRAME**‘