Sidechair

str.replace()

Browse the reference

Replaces one occurrence of a target text, the first by default, with a replacement.

Try it in the editor

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

Pine Script
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 string

Arguments

NameTypeDefaultDescription
sourceconst/simple/series stringrequiredThe text to change. Only one occurrence of target is replaced, chosen by occurrence.
targetconst/simple/series stringrequiredThe piece of text to find and swap out. The match is case sensitive.
replacementconst/simple/series stringrequiredThe text to put in its place.
occurrenceconst/simple/series int0Which match to replace, counting from 0.

Returns

const/simple/series string: the edited text.

Remarks

Example

Pine ScriptReplace the second dashOpen in editor
//@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