get String length in Pine script

2.4k views Asked by At

I have a string nd dynamically assigning values to that string, I want to trigger the alert when the string content changes. I can't find any option to get the string length in pine script. Can you please suggest me. Thanks in advance.

2

There are 2 answers

0
e2e4 On

There is no built-in function to extract the string length, but you can monitor if the string variables changed during the runtime by comparing current state with the previous one. However you'll be restricted to 1 time change / bar, or you'll have to create different monitor functions after each part of the code that could change the variable.

example:

//@version=4
study("String Changed!")

var string someString = ""

bool changeTrigger = crossover(sma(close, 5), sma(close, 10))

if changeTrigger
    someString := someString + " add"

bool stringHaveChanged = someString != someString[1]

bgcolor(stringHaveChanged ? color.red : na)
0
Rick Falck On

you can get the length of a string with the built in function str.length(string) It is in their documentation.