Logic coded to search a string for a specific character and replace that character with another character when found.

Document ID : KB000056423
Last Modified Date : 14/02/2018
Show Technical Document Details
The following example program provides this logic:


***********************************************************************
* THIS IS AN EXAMPLE OF INSPECTING EVERY BYTE FOR A CHARACTER *
* AND REPLACING THAT CHARACTER WITH A NEW CHARACTER IN A NEW FIELD *
***********************************************************************
DEFINE FROM-FIELD W 15 A VALUE 'GOHN '
DEFINE FROM-FIELD-BYTE FROM-FIELD 1 A OCCURS 15
DEFINE FROM-SUB W 2 N
***********************************************************************
* THE TO-FIELD ARRAY USES INDEXING *
***********************************************************************
DEFINE TO-FIELD W 15 A
DEFINE TO-FIELD-BYTE TO-FIELD 1 A OCCURS 15 INDEX TO-INDEX
DEFINE FIND-FIELD W 1 A VALUE 'G'
DEFINE CHANGE-TO W 1 A VALUE 'J'
*
JOB INPUT NULL
MOVE ' ' TO TO-FIELD FILL ' '
FROM-SUB = 1
TO-INDEX = 0
*
DO UNTIL FROM-SUB EQ 16
IF FROM-FIELD-BYTE (FROM-SUB) = FIND-FIELD
TO-FIELD-BYTE (FROM-SUB) = CHANGE-TO
ELSE
TO-FIELD-BYTE = FROM-FIELD-BYTE (FROM-SUB)
END-IF
TO-INDEX = TO-INDEX + 1
FROM-SUB = FROM-SUB + 1
END-DO
*
DISPLAY 'RESULTS:'
DISPLAY ' '
DISPLAY '**********************************************************'
DISPLAY 'FROM-FIELD = ' FROM-FIELD
DISPLAY '*************000000000111111******************************'
DISPLAY ' 123456789012345******************************'
DISPLAY '**********************************************************'
DISPLAY 'TO-FIELD = ' TO-FIELD
DISPLAY '*************000000000111111******************************'
DISPLAY ' 123456789012345******************************'
DISPLAY '**********************************************************'
*
STOP
*
Running this program produces:

RESULTS:

**********************************************************
FROM-FIELD = GOHN
*************000000000111111******************************
123456789012345******************************
**********************************************************
TO-FIELD = JOHN
*************000000000111111******************************
123456789012345******************************
**********************************************************