Home » String Functions » Understanding the str.replace() Function in Pine Script

Understanding the str.replace() Function in Pine Script

Photo of author
Published on

In this article we’ll delve into the str.replace() function, an essential tool for string manipulation that allows you to replace occurrences of a substring within a string. This article will cover its syntax, overloads, and practical examples to enhance your Pine Script programming skills.

Syntax and Overloads

The str.replace() function in Pine Script is versatile, supporting multiple data types for its arguments. Here’s a breakdown of its syntax and the types of strings it can return:

Syntax

str.replace(sourceStr, targetStr, replacementStr, occurrenceN) → const string
str.replace(sourceStr, targetStr, replacementStr, occurrenceN) → simple string
str.replace(sourceStr, targetStr, replacementStr, occurrenceN) → series string

Arguments

  • sourceStr (const string): The original string from which you want to replace a substring.
  • targetStr (const string): The substring you aim to replace.
  • replacementStr (const string): The string that will replace the targetStr.
  • occurrenceN (const int): The N-th occurrence of the targetStr to replace. It is an optional argument with a default value of 0, indicating the first match.

Example: Using str.replace() in an Indicator

To understand how to use the str.replace() function, let’s walk through a practical example. In this scenario, we’re modifying the source string by replacing the first occurrence of a specified substring.

Scenario

Imagine you’re working with financial data and need to switch the data source in your string from “FTX” to “BINANCE”. Here’s how you can do it:

Code Snippet

//@version=5
indicator("str.replace Example",overlay = true)
var sourceStr = "FTX:BTCUSD / FTX:BTCEUR"

// Replace the first occurrence of "FTX" with "BINANCE"
var newSourceStr = str.replace(sourceStr, "FTX",  "BINANCE", 0)

if barstate.islastconfirmedhistory
    // Display "BINANCE:BTCUSD / FTX:BTCEUR"
    label.new(bar_index, high, text = newSourceStr)
Explanation

Explanation

  1. Initialization: We start by declaring the sourceStr variable that contains our original string.
  2. Replacement: The str.replace() function is used to create a new string newSourceStr, where the first occurrence of “FTX” is replaced with “BINANCE”.
  3. Display: If the current bar is the last confirmed history bar, we display the updated string as a label on the chart.

Key Features and Takeaways

  • Function Overloads: The str.replace() function can return constant, simple, or series strings, making it extremely versatile for different scenarios in Pine Script.
  • Flexibility in Replacement: You can specify which occurrence of the target string to replace, offering precise control over string manipulation.
  • Ease of Use: This function simplifies the process of modifying strings, which is especially useful when dealing with dynamic data sources or formatting requirements.

By mastering the str.replace() function, you can efficiently manipulate string data in your Pine Script indicators and strategies, enhancing their flexibility and utility. Whether you’re adjusting data sources, formatting text, or performing conditional replacements, str.replace() is an invaluable tool in your Pine Script toolbox.

Leave a Comment