In this article, we explore how to effectively remove the starting substring, or “prefix,” from a string in Pine Script, enhancing the versatility of string handling in your scripts.
Custom Function to Remove String Prefix
To facilitate the removal of a prefix from a string, we introduce the CustomStrRemovePrefix
function. This utility function is designed to strip a specified prefix from the start of a given source string and can optionally ignore case differences, providing a flexible solution for various string manipulation scenarios.
// CustomStrRemovePrefix() returns a new string with the given prefix (start) // of the specified source string removed, optionally ignoring case // differences between the two strings. CustomStrRemovePrefix(string source, string prefix, bool ignoreCase=false) => modifiedSource = ignoreCase ? str.lower(source) : source modifiedPrefix = ignoreCase ? str.lower(prefix) : prefix if str.startswith(modifiedSource, modifiedPrefix) str.substring(source, str.length(prefix)) else source
This function takes three parameters:
source
: The string from which you want to remove the prefix.prefix
: The substring you intend to remove from the start of thesource
string.ignoreCase
(optional): A boolean flag that, when set totrue
, makes the function ignore case differences between thesource
andprefix
strings. Its default value isfalse
.
Inside the function, we prepare two variables, modifiedSource
and modifiedPrefix
, which hold the source
and prefix
strings respectively. These may be converted to lowercase if ignoreCase
is true
, ensuring case-insensitive comparison when required.
The core of the function lies in its condition-checking mechanism, which employs the str.startswith()
function to determine if the source
string begins with the specified prefix
. If this condition is met, the str.substring()
function is invoked to extract and return the portion of the source
string following the prefix
. Otherwise, the original source
string is returned untouched.
Example
Let’s apply the CustomStrRemovePrefix
function within a practical script to demonstrate its utility. The following indicator script removes a specific prefix from the description of the financial instrument displayed on the chart:
//@version=5 indicator(title="Remove String Prefix - Example", overlay=true) CustomStrRemovePrefix(string source, string prefix, bool ignoreCase=false) => modifiedSource = ignoreCase ? str.lower(source) : source modifiedPrefix = ignoreCase ? str.lower(prefix) : prefix if str.startswith(modifiedSource, modifiedPrefix) str.substring(source, str.length(prefix)) else source cleanedDescription = CustomStrRemovePrefix(syminfo.description, "ProShares", true) if barstate.islastconfirmedhistory label.new(bar_index, high, text="Original Description: " + syminfo.description + "\nCleaned Description: " + cleanedDescription, color=color.teal, textcolor=color.white)
Walkthrough of Code
- Indicator Setup:
//@version=5
: Specifies the version of Pine Script used, which is version 5 in this case.indicator(title="Remove String Prefix - Example", overlay=true)
: Declares a new indicator with the title “Remove String Prefix – Example”. Theoverlay=true
parameter ensures that this indicator is displayed over the price chart.
- Custom Function Declaration –
CustomStrRemovePrefix
:- This function is designed to remove a specified prefix from a given string. It takes three parameters:
source
(the string to be modified),prefix
(the string to be removed), andignoreCase
(a boolean flag to ignore case differences, defaulted tofalse
). - Inside the function, two local variables
modifiedSource
andmodifiedPrefix
are defined. These hold thesource
andprefix
strings respectively, potentially converted to lowercase ifignoreCase
is set totrue
, allowing for case-insensitive comparison. - The function checks if the
modifiedSource
string starts withmodifiedPrefix
usingstr.startswith()
. If true, it returns a substring ofsource
usingstr.substring()
, starting from the character index immediately after the prefix. If false, it returns the originalsource
string unaltered.
- This function is designed to remove a specified prefix from a given string. It takes three parameters:
- Application of the Custom Function:
cleanedDescription = CustomStrRemovePrefix(syminfo.description, "ProShares", true)
: This line calls theCustomStrRemovePrefix
function, applying it to the current financial instrument’s description (syminfo.description
). The function is tasked with removing the “ProShares” prefix from the description, withignoreCase
set totrue
to ignore case differences.
- Displaying Results:
- The final part of the script uses an
if
statement to check if the script is executing on the last confirmed historical bar (barstate.islastconfirmedhistory
). If true, it creates a new label on the chart usinglabel.new()
. - This label displays both the original description and the cleaned description (with the “ProShares” prefix removed). The label is positioned at the
bar_index
andhigh
of the last bar, with a teal background and white text for visibility.
- The final part of the script uses an
Key Features and Takeaways
- Custom Functionality: Pine Script lacks built-in support for removing prefixes from strings, necessitating custom function implementations like
CustomStrRemovePrefix
. - Case Sensitivity Control: The optional
ignoreCase
parameter provides flexibility in handling case sensitivity, accommodating scenarios where case differences should be ignored. - Versatile String Manipulation: Through the strategic use of string functions such as
str.startswith()
andstr.substring()
, Pine Script developers can implement sophisticated string manipulation techniques.
By mastering these string manipulation techniques in Pine Script, developers can enhance the functionality and user experience of their custom indicators and strategies, showcasing the power of custom programming solutions in financial chart analysis.