Join and split
The tools library provides some custom array and string methods that expand Pinescript's own array.join()
and str.split()
methods.
Please make sure to import the
tools
library in your script, before following along with the examples.Join
The _join()
method is similar to the Pinescript's array.join()
method,
and extends the ability to join boolean and color arrays as well.
// Joining array<string>
strArray = array.from("apple", "banana", "carrot")
strArray._join() // returns the string "apple,banana,carrot".
strArray._join(sep=" ") // returns the string "apple banana carrot".
strArray._join(sep="-") // returns the string "apple-banana-carrot".
// Joining array<float>
fltArray = array.from(1.2, 2.1, 3.098, 4.0089)
fltArray._join() // returns the string "1.2,2.1,3.098,4.0089".
fltArray._join(sep=" ") // returns the string "1.2 2.1 3.098 4.0089".
fltArray._join(sep="-") // returns the string "1.2-2.1-3.098-4.0089".
// Joining array<int>
intArray = array.from(1, 2, 3, 4, 5)
intArray._join() // returns the string "1,2,3,4,5".
intArray._join(sep=" ") // returns the string "1 2 3 4 5".
intArray._join(sep="-") // returns the string "1-2-3-4-5".
// Joining array<bool>
boolArray = array.from(true, false, false, true, false)
boolArray._join() // returns the string "true,false,false,true,false".
boolArray._join(sep=" ") // returns the string "true false false true false".
boolArray._join(sep="-") // returns the string "true-false-false-true-false".
// Joining array<color>
clrArray = array.from(color.red, color.black, color.white)
clrArray._join() // returns the string "255|82|82|0,54|58|69|0,255|255|255|0".
clrArray._join(sep=" ") // returns the string "255|82|82|0 54|58|69|0 255|255|255|0".
clrArray._join(sep="-") // returns the string "255|82|82|0-54|58|69|0-255|255|255|0".
Split
The tools library provides several convenient split methods that work similarly to the Pinescript's
str.split()
method.
These methods are named with their separator (comma, pipe or colon) and follow a naming convention:
- Split into an array.
- Split and get just a single item.
- Split into a single tuple pair of first two items.
- Split into an array without the first item.
// Separator: Comma
commaStr = "apple,banana,carrot"
commaStr._commaSplit() // returns an array<string>("apple", "banana", "carrot").
commaStr._commaSplit(idx=2) // returns the string "carrot".
commaStr._commaSplitPair() // returns a tuple containing ["apple", "banana"].
commaStr._commaSplitShift() // returns an array<string>("banana", "carrot") without "apple".
// Separator: Pipe
pipeStr = "apple|banana|carrot"
pipeStr._pipeSplit() // returns an array<string>("apple", "banana", "carrot").
pipeStr._pipeSplit(idx=2) // returns the string "carrot".
pipeStr._pipeSplitPair() // returns a tuple containing ["apple", "banana"].
pipeStr._pipeSplitShift() // returns an array<string>("banana", "carrot") without "apple".
// Separator: Colon
colonStr = "apple:banana:carrot"
colonStr._colonSplit() // returns an array<string>("apple", "banana", "carrot").
colonStr._colonSplit(idx=2) // returns the string "carrot".
colonStr._colonSplitPair() // returns a tuple containing ["apple", "banana"].
colonStr._colonSplitShift() // returns an array<string>("banana", "carrot") without "apple".