substitute, substitute all
The function substitute substitutes the first occurrence of the specified old contents by new contents.
The function substitute all substitutes all occurrences of the specified old contents by new contents.
Comparing wtih the replace() function, the 1st parameter is an input-output parameter where the contents are modified:
substitute( a[], "Hello", "Hi" ); is the same as a[] = replace( a[], "Hello", "Hi" );
Note:In case you want to use vectorization, please use replace resp. replace all instead.
Indirect parameter passing is disabled
3-4
No. | Type | Description |
---|---|---|
1 io |
string | input string Destination string with contents to substitute |
2 input |
set or string | existing contents If provided as a string: The string will be looked for this content. |
3 input |
set or string | new contents These new contents will be inserted. |
Opt. 4 input |
numeral | starting position String substitution begins at a specified characcter position. For example, 2 skips the first two chracters in the input string. Default value: 0 |
3rd parameter contains different number of elements than 2nd parameter, and is not 1
original[] = "the house, the mouse, and the spouse";
st1[] = original[];
st2[] = original[];
substitute ( st1[], the, a );
substitute all ( st2[], the, a );
echo(st1[]);
echo(st2[]);
st1[] = original[];
st2[] = original[];
substitute ( st1[], { the, spouse, mouse, house }, { THE, SPOUSE, MOUSE, HOUSE } );
substitute all ( st2[], { the, spouse, mouse, house }, { THE, SPOUSE, MOUSE, HOUSE } );
echo(st1[]);
echo(st2[]);
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