TradingCharts

Strings

Strings are 16-bit Unicode. Both 'single' and "double" quote delimiters work; both support the same escape sequences:

Concatenation

+ joins two strings. Mixed types do not concatenate automatically:

msg = "Close: " + str.tostring(close)

Conversion

Call Result
str.tostring(x) Decimal representation.
str.tostring(x, format) Formatted. See Format strings.
str.tonumber(s) float from a numeric string ("3.14" → 3.14). Returns na on failure.
int(x) / float(x) / bool(x) Type coercion.

Format strings

str.tostring(value, format) and str.format(template, …) accept both the modern format.* constants and #.## patterns:

str.tostring(123.456,  format.price)       // "123.46"
str.tostring(123.456,  format.volume)      // "123.456"
str.tostring(0.1234,   format.percent)     // "12.34%"
str.tostring(123.4,    format.mintick)     // rounded to syminfo.mintick (0.05 → "123.40")

str.tostring(123.456,  "#.##")             // "123.46"
str.tostring(123.456,  "0.0000")           // "123.4560"
str.tostring(1234567,  "#,##0")            // "1,234,567"

str.format(template, …)

msg = str.format("Price {0} at {1}", close, syminfo.ticker)

Positional substitutions use {0}, {1}, …; a width/precision suffix can be added: "{0,.2f}", "{0,8}".

str.format_time(epochMs, format?, timezone?)

when = str.format_time(time, "yyyy-MM-dd HH:mm", "Asia/Kolkata")

Default format is "yyyy-MM-dd'T'HH:mm:ssZ" and, like every time function on the platform, it formats in IST (Asia/Kolkata) unless a timezone is given.

Query functions

Call Returns
str.length(s) character count
str.contains(s, needle) bool
str.startswith(s, prefix) bool
str.endswith(s, suffix) bool
str.pos(s, needle) 0-based index, or -1
str.lower(s) / str.upper(s) case conversion
str.split(s, sep) array<string>
str.replace(s, needle, repl, nth?) replaces the nth occurrence (default: first)
str.replace_all(s, needle, repl) all-occurrences replace
str.trim(s) strips leading / trailing whitespace
str.match(s, regex) first match of a regular expression, or ""
str.repeat(s, n, separator?) n copies, joined by separator (default "")
str.substring(s, from, to?) half-open slice

Method form

Every function above is also callable as a method on the string:

"Hello world".upper()           // "HELLO WORLD"
"a,b,c".split(",")              // ["a","b","c"]
"abcabc".replace_all("a", "X")  // "XbcXbc"

Concatenation with alerts and tables

alert("Breach on " + syminfo.ticker + " · " + str.tostring(close, format.price),
      alert.freq_once_per_bar_close)

See also