Let’s delve deeper into the syntax, usage, and practical applications of str.trim()
in Pine Script.
Syntax and Overloads
The str.trim()
function comes with a straightforward syntax and multiple overloads to accommodate different types of strings, making it versatile for various scripting scenarios. Here’s a breakdown of its syntax and overloads:
str.trim(source) → const string
str.trim(source) → input string
str.trim(source) → simple string
str.trim(source) → series string
Arguments:
- source (
const string
): The string you intend to trim. This argument can take various forms of strings, including constant strings, input strings, simple strings, and series strings, depending on your specific needs.
Example
To understand how str.trim()
works in practice, let’s consider a basic example:
//@version=5 indicator("str.trim Example", overlay=true) trimmedText = str.trim(" Pine Wizards ") // Returns "Pine Wizards" Label = label.new(bar_index, close, text=trimmedText, style=label.style_label_down) label.delete(Label[1])
Walkthrough of Code
Certainly! Let’s break down the provided Pine Script code piece by piece to understand its functionality and how each line contributes to its operation:
- Indicator Declaration:
//@version=5 indicator("str.trim Example", overlay=true)
//@version=5
: Specifies that this script uses version 5 of Pine Script, ensuring it utilizes the syntax and features specific to this version.indicator("str.trim Example", overlay=true)
: This line declares a new indicator named “str.trim Example”. Theoverlay=true
parameter specifies that the indicator will be drawn over the price chart, allowing for direct interaction with the chart elements.
- Trimming a String:
trimmedText = str.trim(" Pine Wizards ") // Returns "Pine Wizards"
trimmedText = str.trim(" Pine Wizards ")
: Uses thestr.trim()
function to remove all leading and trailing whitespace from the string" Pine Wizards "
. The result, which is"Pine Wizards"
, is assigned to the variabletrimmedText
.
- Creating a Label:
Label = label.new(bar_index, close, text=trimmedText, style=label.style_label_down)
Label = label.new(bar_index, close, text=trimmedText, style=label.style_label_down)
: This line creates a new label on the chart at the current bar’s index position (bar_index
) and at the price level corresponding toclose
. The text of the label is set to thetrimmedText
variable’s value, which is"Pine Wizards"
. Thestyle=label.style_label_down
parameter specifies that the label’s anchor point will be at the bottom of the label box, pointing downwards.
- Deleting the Previous Label:
label.delete(Label[1])
label.delete(Label[1])
: Deletes the label created one bar before the current one.Label[1]
refers to the label instance from the previous bar because[1]
is used to access the previous value of a series.
Key Features and Takeaways
- Function Versatility: The
str.trim()
function supports multiple types of strings, including constant, input, simple, and series strings. This makes it a versatile tool for string manipulation in various contexts within Pine Script. - Syntax Simplicity: With a straightforward syntax,
str.trim(source)
, the function is easy to use and incorporate into your scripts, enhancing code readability and maintainability. - Practical Application: Use
str.trim()
to prepare strings for display or further processing, ensuring that your script outputs are clean and user-friendly. - Edge Case Handling: The function’s behavior when returning an empty string or dealing with
na
values ensures robust script performance, even in scenarios where the input might be less predictable.