INSPECT REPLACING Statement


INSPECT...REPLACING performs character replacement in a variable. It's a way to replace all instances of certain characters with other characters. It works on individual characters in the group of characters.

Syntax -

* Replacing all characters
INSPECT   ws-input-string 
REPLACING CHARACTERS 
           BY {ws-replacing-char|replacing-literal}. 

* Replacing all characters
* with BEFORE|AFTER|INITIAL
INSPECT   ws-input-string
REPLACING CHARACTERS 
           BY {ws-replacing-char|replacing-literal}
          [[BEFORE|AFTER] [INITIAL] 
		  {ws-delimeter|delimeter-literal}]
		  
* Replacing specific occurrences of characters
INSPECT   ws-input-string
REPLACING [ALL|LEADING|FIRST] 
              {ws-replaced-char|replaced-literal}
	       BY {ws-replacing-char|replacing-literal}

* Replacing specific occurrences of characters 
* with BEFORE|AFTER|INITIAL
INSPECT   ws-input-string
REPLACING [ALL|LEADING|FIRST]  
              {ws-replaced-char|replaced-literal}
	       BY {ws-replacing-char|replacing-literal}
	      [[BEFORE|AFTER] [INITIAL] 
		  {ws-delimeter|delimeter-literal}]
Note! All statements coded in [ ] are optional.

Parameters -

  • ws-input-string - The data item that will be inspected for replacements. It should be an alphanumeric variable declared with usage DISPLAY.
  • ws-replaced-char|replaced-literal - The character(s) we're searching for in ws-input-string to replace.
  • ws-replacing-char|replacing-literal - The character(s) that will replace the ws-replaced-char.
  • BEFORE ws-delimeter - Specifies replacing begins at the leftmost character position and continues till the first occurrence of the delimiter. If no delimiter is found, replacing continues until the last character of the string.
  • AFTER ws-delimeter - Specifies replacing begins at the first character to the right of delimiter and continues until the last character of the string. If no delimiter is found, no replacement takes place.
  • INITIAL - Specifies the first occurrence of a delimeter.
  • ws-delimeter|delimeter-literal - Specifies the delimeter alphanumeric variable or literal.
  • CHARACTERS BY -
    • Each character is replaced by a replacing character (includes space) when CHARACTERS BY alone coded (without BEFORE or AFTER phrases).
    • If the BEFORE or AFTER is coded, all the character are replaced by a replacing character depending on BEFORE or AFTER ws-delimeter.
  • ALL -
    • Each occurrence of ws-replaced-char is replaced by a replacing character (includes space) when ALL alone coded (without BEFORE or AFTER phrases).
    • If the BEFORE or AFTER is coded along with ALL, the replacement happens depending on BEFORE or AFTER ws-delimeter.
  • LEADING -
    • Replaces leftmost occurrence of ws-replaced-char is replaced by a replacing character (includes space) when LEADING alone coded (without BEFORE or AFTER phrases).
    • If the BEFORE or AFTER is coded along with LEADING, the replacement happens depending on BEFORE or AFTER ws-delimeter.
  • FIRST -
    • Replaces leftmost first occurrence of ws-replaced-char is replaced by a replacing character (includes space) when FIRST alone coded (without BEFORE or AFTER phrases).
    • If the BEFORE or AFTER is coded along with FIRST, the replacement happens depending on BEFORE or AFTER ws-delimeter.

Examples -


Scenario1 - Replace all "-" with "/".

Input-        WS-DATA = "DD-MM-YYYY"
Declaration-  05 WS-DATA        PIC X(10) VALUE "DD-MM-YYYY".
Code-         INSPECT WS-DATA 
                      REPLACING ALL "-" BY "/". 
Result-       WS-DATA = "DD/MM/YYYY"

Scenario2 - Replace characters with '$'.

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA        PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA 
                      REPLACING CHARACTERS BY "$".   
Result-       WS-DATA = "$$$$$$$$$$"

In the above case, every character is replaced by '$'. So the result is "$$$$$$$$$$".

Scenario3 - Replace characters with '$' before character "R".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA       PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA 
                      REPLACING CHARACTERS BY "$" 
                      BEFORE "R".   
Result-       WS-DATA = "$$$$$RAMES"

In the above case, every character before "R" is replaced by '$'. So the result is "$$$$$RAMES".

Scenario4 - Replace characters with '$' after character "R".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA      PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA 
                      REPLACING CHARACTERS BY "$" 
                      AFTER "R".   
Result-       WS-DATA = "MAINFR$$$$"

In the above case, every character after "R" is replaced by '$'. So the result is "MAINFR$$$$".

Scenario5 - Replace characters with '$' before first occurrence of "A".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA      PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA 
                      REPLACING CHARACTERS BY "$" 
                      BEFORE INITIAL "A".   
Result-       WS-DATA = "$AINFRAMES"

In the above case, every character before first occurrence of "A" is replaced by '$'. So the result is "$AINFRAMES".

Scenario6 - Replace characters with '$' after first occurrence of "A".

Input-        WS-DATA = "MAINFRAMES"
Declaration-  05 WS-DATA      PIC X(10) VALUE "MAINFRAMES".
Code-         INSPECT WS-DATA 
                      REPLACING CHARACTERS BY "$" 
                      AFTER INITIAL "A".   
Result-       WS-DATA = "MA$$$$$$$$"

In the above case, every character after first occurrence of "A" is replaced by '$'. So the result is "MA$$$$$$$$".

Practical Example -


Scenario - INSPECT REPLACING statement usage for multiple replacements in COBOL programming.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. INSPECTR.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01 WS-VAR.
          05 WS-DATA    PIC X(50) VALUE
             "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE".

       PROCEDURE DIVISION. 
 
           INSPECT WS-DATA 
                   REPLACING ALL SPACES BY "#" AFTER "LEVEL"
                            "LANGUAGE" BY "&&&&&&&&" BEFORE "HIGH"
                            "IS" BY "**".
           DISPLAY "AFTER REPLACING:   " WS-DATA.
           STOP RUN.

Output -

INSPECT REPLACING program Output

Explaining Example -

In the above case,

  • SPACES AFTER "LEVEL" replaced by "#".
  • "LANGUAGE" before "HIGH" replaced by "&&&&&&&&".
  • "IS" replaced by "**".

After replacements, the result is "COBOL &&&&&&&& ** A HIGH LEVEL#LANGUAGE###########".