Skip to Content

How to replace a text within a string with a different length of text.

Estimated Reading Time: 1 Minutes

Usually to replace a text in a string you use the "inspect replacing" statement. But with this kind of approach the original text and the new text must have the same length.
To avoid this problem you can use the "C$REPLACE_ALL" routine.
This routine uses the regular expression syntax in both the search string and the replace string.
Here's the syntax for C$REPLACE_ALL:

  call "C$REPLACE_ALL" using wrk-str 
                             search-string 
                             replace-string
                             crep-right-trimmed 
                             w-error
                      giving wstatus
Where:
"wrk-str" contains the full string passed and returned from the routine.
"search-string" is the regular expression used to find the text to be replaced.
"replace-string" is a variable that contains the regular expression for the new text.
"crep-right-trimmed" is a constant used to change the behavior of the routine. The possible value can be "CREP_CASE_INSENSITIVE", "CREP_LEFT_TRIMMED" and "CREP_RIGHT_TRIMMED"
"w-error" is a variable that contains the error description if there is one.
"wstatus" is a variable that contains the status of the execution.

For example, to change the string "Today is a rainy day" to "Today is a very sunny day"

  move "Today is a rainy day"   to wrk-str
  call "C$REPLACE-ALL" using wrk-str 
                             "rainy"
                             "very sunny"
                             crep-right-trimmed 
                             w-error
                      giving wstatus

How to replace a text within a string with a different length of text.

Powered by PHPKB (Knowledge Base Software)