Home » String Functions » Removing the End (Suffix) from a Pine Script String

Removing the End (Suffix) from a Pine Script String

Photo of author
Published on

In this article, we will explore how to custom-code a function to remove the end (suffix) from a string in Pine Script, enhancing our script’s capability to clean up string data efficiently.

Custom Function to Remove String Suffix

To address the absence of a direct function for removing a suffix from a string in Pine Script, we can create a custom function called StrTrimSuffix. This function aims to remove a specified substring from the end of a source string, with an option to ignore case differences, making it highly flexible for various use cases.

Function Definition:

// StrTrimSuffix() returns a new string with the given suffix (end)
// of the specified source string removed, optionally ignoring case
// differences between the two strings.
StrTrimSuffix(string source, string suffix, ignoreCase=false) =>
    processedSource = ignoreCase ? str.lower(source) : source
    processedSuffix = ignoreCase ? str.lower(suffix) : suffix

    if str.endswith(processedSource, processedSuffix)
        str.substring(source, 0, str.length(source) - str.length(suffix))
    else
        source

This function, StrTrimSuffix, takes three parameters:

  • source: The original string from which we aim to remove the suffix.
  • suffix: The substring that, if found at the end of source, will be removed.
  • ignoreCase: An optional boolean parameter that determines whether to ignore case differences when comparing source and suffix. Its default value is false.

The function first converts both the source and suffix strings to lowercase if ignoreCase is set to true, ensuring case-insensitive comparison. It then checks if source ends with suffix using the str.endswith() function. If true, it removes the suffix from source by returning a substring of source that excludes the suffix length. If source does not end with suffix, it returns the original source string unchanged.

Example: Cleaning Instrument Descriptions

To illustrate the practical application of the StrTrimSuffix function, consider an example where we need to clean the description of a financial instrument by removing potential “future” or “futures” suffixes, ensuring a cleaner and more concise description.

Script Implementation:
//@version=5
indicator(title="Clean Instrument Description Example", overlay=true)

StrTrimSuffix(string source, string suffix, ignoreCase=false) =>
    processedSource = ignoreCase ? str.lower(source) : source
    processedSuffix = ignoreCase ? str.lower(suffix) : suffix
    
    if str.endswith(processedSource, processedSuffix)
        str.substring(source, 0, str.length(source) - str.length(suffix))
    else
        source

cleanDescription = StrTrimSuffix(syminfo.description, "futures", true)
cleanDescription := StrTrimSuffix(cleanDescription, "future", true)

if barstate.islastconfirmedhistory
    label.new(bar_index, high, text="Original Description:\n" + syminfo.description + 
              "\n\nCleaned Description:\n" + cleanDescription, color=color.navy, textcolor=color.white)
Script Implementation:

Walkthrough of Code 

  1. Definition of StrTrimSuffix Function:
    • Purpose: This function is designed to remove a specified suffix from a given string. It includes an option to ignore case sensitivity during the process.
    • Parameters:
      • source: The original string from which the suffix will be removed.
      • suffix: The suffix to be removed from the source string.
      • ignoreCase: A boolean parameter that determines whether the case should be ignored. It defaults to false.
  2. Case Handling:
    • Inside the function, it first checks if case sensitivity should be ignored (ignoreCase). If so, both the source string and the suffix are converted to lowercase to ensure case-insensitive comparison.
  3. Suffix Removal:
    • The function then checks if the processed source string ends with the processed suffix. If true, it removes the suffix by using str.substring to keep the part of the source string before the suffix. Otherwise, it returns the original source string unaltered.
  4. Usage of StrTrimSuffix Function for Cleaning Description:
    • The function is called twice with syminfo.description as the source string, first to remove "futures" and then to remove "future" from the instrument’s description, considering case insensitivity. This is to ensure that both possible suffixes are removed regardless of case.
  5. Displaying Original and Cleaned Descriptions:
    • The script checks if the current bar is the last confirmed historical bar using barstate.islastconfirmedhistory. If true, it creates a new label on the chart at the high price of the current bar. This label contains both the original and the cleaned descriptions of the instrument, using syminfo.description and cleanDescription variables, respectively.
    • The label is styled with a navy color for the background and white text for visibility.

Key Features and Takeaways

  • The StrTrimSuffix function provides a custom solution for removing suffixes from strings in Pine Script, enhancing string manipulation capabilities.
  • It supports case-insensitive operations, offering flexibility in handling string comparisons.
  • The example script demonstrates the function’s practical application in cleaning financial instrument descriptions, a common task in financial analysis and chart annotations.

By creating and utilizing the StrTrimSuffix function, Pine Script users can efficiently manage and manipulate string data, overcoming the limitations of the language’s built-in string functions. 

Leave a Comment