Pine Script

How to concatenate strings in Pine Script

How to concatenate strings in Pine Script

String concatenation is the main string feature in any programming language. It allows multiple strings to be joined together to form one larger string. This is used in many programming languages to create more complicated strings. String concatenation can be used to combine strings of different lengths, as well as to concatenate strings of text and numerical data.

Advanced methods to work with strings were added to Pine Script quite recently. Now there are a few ways to concatenate strings in Pine Script. In this article, I’ll show you a few examples.

If we want simply concatenate a few strings, we can use the “+” operator for it:

str1 = "Hello"  + "World"  
str2 = "Hello"  + "World"  + "!"

When you try to add a number or any other type to a string there, you might have an issue. The following code will give you an error:

str3 = "Current Close: " + close
11:52:37 AM(Error at 11:8) Cannot call ‘operator +’ with argument ‘expr0’=’Current Close: ‘. An argument of ‘literal string’ type was used, but a ‘series float’ is expected

To fix this, you have to use “str.tostring” function to transform all types to string first and only then concatenate. Something like this works fine:

str3 = "Current Close: " + str.tostring(close)

If you want to concatenate multiple strings together at the same time with the same separator in Pine Script, you can take a look at array.join function. You have to pass to it arrays of strings or any other types and separators you want to use. It will join all the elements and will output you one string with all the elements:

a = array.from(1, 2, 3, 4, 5)

if barstate.islast
    label.new(bar_index, close, array.join(a, "-"))

Result will look like this:


Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news