Home » String Functions » Understanding str.length() Function in Pine Script

Understanding str.length() Function in Pine Script

Photo of author
Published on

In Pine Script, the str.length function is a fundamental string operation that allows developers to determine the length of a string, that is, the number of characters it contains. In this article, we will explore how to use str.length in Pine Script Version 5, with practical examples to guide you through its applications.

Using str.length in Pine Script


The syntax for str.length is straightforward:

length = str.length(sourceString)
  • sourceString: This is the string variable or literal whose length you want to determine.
  • length: This will hold the length of the string, expressed as an integer.


indicator("String Length Example", overlay=true)

// Example string, correctly declared and assigned
var string myText = "Pine Wizards" // Declare and initialize myText with a string value

// Calculate the length of the string
var int textLength = str.length(myText) // Declare and assign textLength

// Plot the length on the chart
label.new(bar_index, high, "Length: " + str.tostring(textLength), color=color.red)

Detailed Walkthrough

  • Version Specification:
    • The script begins with //@version=5, indicating it uses Pine Script Version 5 syntax and features.
  • Indicator Setup:
    • indicator("String Length Example", overlay=true) defines a new indicator named “String Length Example” that will be drawn over the price chart (overlay=true).
  • String Declaration and Initialization:
    • var string myText = "Pine Wizards" declares a variable myText of type string, using the var keyword to ensure its value is retained across chart bars. It’s initialized with the string “Pine Wizards”.
  • String Length Calculation:
    • var int textLength = str.length(myText) calculates the length of myText using the str.length function. The result is stored in the variable textLength, declared as an integer (int), also with var to maintain its value over time.
  • Displaying the Length on the Chart:
    • label.new(bar_index, high, "Length: " + str.tostring(textLength), color=color.red) creates a new label on the chart. This label uses the bar_index for its x-coordinate (placing it on the current bar) and the high price for its y-coordinate, positioning the label above the current bar’s high price. The label text displays “Length: ” followed by the string length, converted to a string for display purposes. The label’s text color is set to red.

Key Features of str.length

  • Usability: str.length is easy to use and requires only a single argument—the string whose length you want to determine.
  • Syntax: It follows a simple syntax, making it accessible even to those new to Pine Script.
  • Application: This function is versatile and can be used in various scenarios, such as validating input data, formatting output, or managing text-based indicators.


  • The str.length function in Pine Script Version 5 is a straightforward yet powerful tool for measuring the length of strings.
  • Its simplicity and ease of use make it an essential function for Pine Script developers.
  • By integrating str.length into your scripts, you can enhance data processing and presentation capabilities in your financial charts.

Leave a Comment