Sidechair

str.substring()

Browse the reference

Returns the part of a string from begin_pos up to, but not including, end_pos.

Try it in the editor

str.substring cuts out a slice of the text. Positions count from 0, so str.substring("chart", 1, 3) returns "ha".

Leave out end_pos to take everything from begin_pos to the end. The begin position has to sit inside the string and the end position cannot be before it or past the end, or the script stops with an error.

Syntax

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

Arguments

NameTypeDefaultDescription
sourceconst/simple/series stringrequiredThe text to cut a piece from. begin_pos and end_pos count its characters from 0.
begin_posconst/simple/series intrequiredIndex of the first character to include. Counts from 0.
end_posconst/simple/series intlength of the stringIndex just after the last character to include.

Returns

const/simple/series string: the slice of text.

Remarks

  • Check bounds with str.length before slicing text that can vary in size.

Example

Pine ScriptFirst two characters of the tickerOpen in editor
//@version=6
indicator("Substring")
head = str.substring(syminfo.ticker, 0, 2)
plot(str.length(head), "Length of head", color = color.blue, style = plot.style_stepline)
plot(str.pos(syminfo.ticker, head), "Position of head", color = color.red)

See also

Runs on Sidechair