Returns a string with one or more incidents of a substring replaced by another substring.
Syntax
REPLACE x,f,t[,n[,i]]
Parameters
- x
- Specifies a string expression of a CHARACTER type within which the incidents of the substring
f are replaced by the substring
t.
- f
- Specifies a string expression of a CHARACTER type that is replaced within the string
x.
- t
- Specifies a string expression of a CHARACTER type that specifies the substring used to replace the substring
f within the string
x.
- n
- Specifies an optional expression of a computational type that identifies a location within the string
x from where the compiler begins searching for the substring
f.
n is converted to FIXED BINARY(31,0).
The default value is
1. When
n <
1 or > the
length(x), the STRINGRANGE condition is raised if enabled, and the result is a null character string.
- i
- Specifies an optional computational-type expression that represents the maximum number of times to replace substring
f by substring
t.
i is converted to FIXED BINARY(31,0).
The value of
i must be a positive number. The default is
1. When
i =
0, all occurrences of the substring
f in the string
x are replaced by the substring
t.
Example
dcl str char (32);
dcl s char (32);
dcl v char (32) var;
dcl (x,y) fixed bin (15);
str = 'abc d';
s = replace(str, ' ', 'x');
put skip data(s);
s = replace(str, ' ', 'x', 1 , 12);
put skip data(s);
s = replace(str, ' ', 'x', 5 , 0);
put skip data(s);
str = 'XXXX';
v = 'ABc';
x = 1;
y = 3;
str = replace(str, substr(str,1,1), v, x, y);
put skip data(str);
Will print:
S='abcxd ' ;
S='abcxdxxxxxxxxxxx ' ;
S='abc dxxxxxxxxxxxxxxxxxxxxxxxxxxx' ;
STR='ABcABcABcX ' ;