string replace
Gives a string with a range of characters replaced by other text.
string replace STRING FIRST LAST ?NEW?
Description
Gives STRING with the characters from FIRST to LAST, both included, replaced by NEW. With no NEW the range goes away. The variable of STRING does not change.
A FIRST below 0 counts as 0, and a LAST past the end counts as the last character. When the range has no character of STRING, such as with FIRST after LAST or FIRST past the end, the result is STRING with no change: NEW does not go in.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| STRING | string | required | The text to change. |
| FIRST | index | required | The index of the first character to replace. |
| LAST | index | required | The index of the last character to replace. |
| NEW | string | "" | The text that goes in place of the range. |
Returns
The new string.
Examples
% string replace abcdef 1 3 X
aXef
% string replace abcdef 1 3
aef
% string replace abcdef end end Z
abcdeZ
% string replace abcdef 4 10 X
abcdX
% string replace abcdef 3 1 X
abcdef
Errors
% string replace a
wrong # args: should be "string replace string first last ?string?"
-errorcode TCL WRONGARGS
% string replace abc x 1 Z
bad index "x": must be integer?[+-]integer? or end?[+-]integer?
-errorcode NONE
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1