Sidechair

str.pos()

Browse the reference

Returns the index where a string first appears inside another, or na if it does not.

Try it in the editor

str.pos(source, str) gives the position of the first match, counting from 0. A match at the very start returns 0.

A missing match returns na, not -1, so test with na() or nz() before using the result as an index.

Syntax

Pine Script
str.pos(source: const string, str: const string) -> const int
str.pos(source: simple string, str: simple string) -> simple int
str.pos(source: series string, str: series string) -> series int

Arguments

NameTypeDefaultDescription
sourceconst/simple/series stringrequiredThe text to look inside. The search starts at the first character.
strconst/simple/series stringrequiredThe piece of text to find. The index of its first match is returned, or na.

Returns

const/simple/series int: the zero-based index of the first match, or na.

Remarks

Example

Pine ScriptSplit at a markerOpen in editor
//@version=6
indicator("Position")
txt = "alpha:beta"
at = str.pos(txt, ":")
plot(at, "Colon at", color = color.red, style = plot.style_stepline)
plot(str.length(str.substring(txt, at + 1)), "Text after", color = color.green, style = plot.style_stepline)

See also

Runs on Sidechair