Replaces one occurrence of a target text, the first by default, with a replacement.
str.replace finds the target in the source and swaps it for the replacement. By default the first match changes and the rest stay.
The occurrence argument picks which match to change, counting from 0, so 1 changes the second one. If there is no such match, the text comes back unchanged.
Syntax
str.replace(source: const string, target: const string, replacement: const string, occurrence: const int (optional)) -> const string
str.replace(source: simple string, target: simple string, replacement: simple string, occurrence: simple int (optional)) -> simple string
str.replace(source: series string, target: series string, replacement: series string, occurrence: series int (optional)) -> series stringArguments
| Name | Type | Default | Description |
|---|---|---|---|
| source | const/ | required | The text to change. Only one occurrence of target is replaced, chosen by occurrence. |
| target | const/ | required | The piece of text to find and swap out. The match is case sensitive. |
| replacement | const/ | required | The text to put in its place. |
| occurrence | const/ | 0 | Which match to replace, counting from 0. |
Returns
const/simple/series string: the edited text.
Remarks
- Use
str.replace_allto change every match at once.
Example
//@version=6
indicator("Replace")
out = str.replace("a-b-c-d", "-", "+", 1)
plot(str.pos(out, "+"), "Plus position", color = color.blue, style = plot.style_stepline)See also
Runs on Sidechair