The str.substring()
function is a powerful tool for extracting portions of strings. This article provides an in-depth look at using str.substring()
in Pine Script.
Syntax & Overloads
The str.substring()
function has multiple overloads, allowing it to work with different string types and arguments. Here’s a breakdown:
str.substring(source, begin_pos) → const string
str.substring(source, begin_pos) → simple string
str.substring(source, begin_pos) → series string
str.substring(source, begin_pos, end_pos) → const string
str.substring(source, begin_pos, end_pos) → simple string
str.substring(source, begin_pos, end_pos) → series string
Arguments
- source (
const string
): The source string from which to extract the substring. - begin_pos (
const int
): The beginning position (inclusive) of the extracted substring. - end_pos (
const int
, optional): The end position (exclusive) of the substring. If not specified, the substring extends to the end of the source string.
Example
Let’s examine a practical example to understand how to use str.substring()
:
//@version=5 indicator("Demo: str.substring", overlay = true) symbol= input.symbol("NASDAQ:AAPL") position = str.pos(symbol, ":") // Get position of ":" character ticker = str.substring(symbol, position+1) // "AAPL" if barstate.islastconfirmedhistory label.new(bar_index, high, text = ticker)
Step-by-Step Walkthrough
- Initialization: The script starts with specifying the Pine Script version and setting the indicator’s name and overlay property.
- Input Symbol: It uses
input.symbol()
to allow the user to input a stock symbol, in this case, “NASDAQ:AAPL”. - Finding the Position:
str.pos(symbol, ":")
is used to find the index position of the colon (“:”) character in the input symbol. - Extracting the Ticker:
str.substring(symbol, position+1)
extracts the substring starting just after the colon, which is the actual ticker symbol, “AAPL”. - Displaying the Ticker: Finally, if the current bar is the last confirmed bar in historical data, it creates a new label displaying the extracted ticker symbol.
Returns
The function returns the extracted substring based on the specified positions. If begin_pos
equals end_pos
, an empty string is returned.
Remarks
- String indexing starts from 0 in Pine Script.
- The
end_pos
argument is exclusive, meaning the character at this position is not included in the extracted substring.
Key Features and Takeaways
- Functionality: The
str.substring()
function is versatile, allowing for the extraction of specific parts of a string, which is especially useful for processing financial symbols or any text-based data. - Syntax Flexibility: With multiple overloads, it supports const, simple, and series string types, making it adaptable to various scenarios.
- Practical Application: Useful in financial analysis for extracting ticker symbols or other relevant data from string inputs.
- Understanding Indexing: It’s crucial to grasp the zero-based indexing and the inclusive-exclusive nature of the position arguments for effective use.
By mastering the str.substring()
function in Pine Script, you can enhance your financial analysis scripts, making them more dynamic and capable of handling complex string manipulation tasks.