INSPECT REPLACING Statement
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}]
Parameters -
- ws-input-string - Specifies the input string or variable.
- ws-replaced-char|replaced-literal - Specifies the characters in string to replace.
- ws-replacing-char|replacing-literal - Specifies the characters that replaces ws-replaced-char.
- BEFORE ws-delimeter - Specifies replacing begins at the leftmost character position and continues untill 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 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 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 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 - Replacing the specific characters in COBOL program.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
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.
...
Output -
BEFORE REPLACING: COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE AFTER REPLACING: COBOL &&&&&&&& ** A HIGH LEVEL#LANGUAGE###########