replace, replace all
The function replace replaces the first occurrence of the specified old contents by new contents.
The function replace all replaces all occurrences of the specified old contents by new contents.
Comparing wtih the substitute() function, the 1st parameter is an input parameter and the return value contains the modified string.
substitute( a[], "Hello", "Hi" ); is the same as a[] = replace( a[], "Hello", "Hi" );
Indirect parameter passing is disabled
3-4
No. | Type | Description |
---|---|---|
1 input |
string | input string Input string with contents to replace |
2 input |
parameter set or string | existing contents If provided as a string: The string will be looked for this content. |
3 input |
parameter set or string | new contents These new contents will be inserted. |
Opt. 4 input |
numeral | starting position String replacement begins at a specified characcter position. For example, 2 skips the first two chracters in the input string. Default value: 0 |
Type | Description |
---|---|
string | String wîth replaced contents |
3rd parameter contains different number of elements than 2nd parameter, and is not 1
st[] = "the house, the mouse, and the spouse";
echo( replace ( st[], the, a ) );
echo( replace all ( st[], the, a ) );
// First occurrence of each term
echo( replace ( st[], { the, spouse, mouse, house }, { THE, SPOUSE, MOUSE, HOUSE } ) );
// All occurrences. See difference on 'the'
echo( replace all ( st[], { the, spouse, mouse, house }, { THE, SPOUSE, MOUSE, HOUSE } ) );
echo( replace all ( st[], { spouse, mouse, house }, { one } ) ); // Replace all by 'home'
echo( replace ( "Café Café Café", Café, Caffè, 1 ) ); // Skip first 'Café'
echo( replace all( "Café Café Café", Café, Caffè, 1 ) ); // Skip first 'Café'
a house, the mouse, and the spouse
a house, a mouse, and a spouse
THE HOUSE, the MOUSE, and the SPOUSE
THE HOUSE, THE MOUSE, and THE SPOUSE
the one, the one, and the one
Café Caffè Café
Café Caffè Caffè