Home » Uncategorized » Removing the Start (Prefix) from a Pine Script String

Removing the Start (Prefix) from a Pine Script String

Photo of author
Published on

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 the source string.
  • ignoreCase (optional): A boolean flag that, when set to true, makes the function ignore case differences between the source and prefix strings. Its default value is false.

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)
Example

Walkthrough of Code 

  1. 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”. The overlay=true parameter ensures that this indicator is displayed over the price chart.
  2. 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), and ignoreCase (a boolean flag to ignore case differences, defaulted to false).
    • Inside the function, two local variables modifiedSource and modifiedPrefix are defined. These hold the source and prefix strings respectively, potentially converted to lowercase if ignoreCase is set to true, allowing for case-insensitive comparison.
    • The function checks if the modifiedSource string starts with modifiedPrefix using str.startswith(). If true, it returns a substring of source using str.substring(), starting from the character index immediately after the prefix. If false, it returns the original source string unaltered.
  3. Application of the Custom Function:
    • cleanedDescription = CustomStrRemovePrefix(syminfo.description, "ProShares", true): This line calls the CustomStrRemovePrefix function, applying it to the current financial instrument’s description (syminfo.description). The function is tasked with removing the “ProShares” prefix from the description, with ignoreCase set to true to ignore case differences.
  4. 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 using label.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 and high of the last bar, with a teal background and white text for visibility.

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() and str.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.

Leave a Comment