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

Understanding the str.replace_all() Function in Pine Script

Photo of author
Published on

The str.replace_all() function is crucial for performing string replacements, allowing you to create more dynamic and customizable Pine Script indicators and strategies.

Syntax & Overloads

The str.replace_all() function is straightforward in its application, offering a simple yet powerful way to manipulate strings. It comes in two main forms, catering to different types of strings in Pine Script: simple strings and series strings. The basic syntax is:

str.replace_all(source, target, replacement) → simple string
str.replace_all(source, target, replacement) → series string

Arguments

  • source (simple string): This is the original string where the replacements will be applied. It can be a hard-coded string or a variable containing string data.
  • target (simple string): The substring that you wish to replace in the source string. Whenever this exact sequence of characters is found, it will be replaced.
  • replacement (simple string): This is the string that will replace each occurrence of the target string within the source string.

Returns

  • Processed string: The function returns a new string where each instance of the target string in the source has been replaced with the replacement string.

Example

Let’s put str.replace_all() into action with a simple example. Suppose we have a string variable that contains the phrase “Hello, World!” and we want to replace “World” with “Pine Script”. Here’s how it can be done:

//@version=5
indicator("Replace Example", overlay=true)

// Original string
greeting := "Hello, World!"

// Using str.replace_all() to replace "World" with "Pine Script"
customGreeting := str.replace_all(greeting, "World", "Pine Script")

// Output the custom greeting
label.new(bar_index, high, customGreeting)
Example

Explanation of the Code

  • We start by declaring the version of Pine Script and setting up our indicator.
  • The greeting variable holds our original string.
  • We then use str.replace_all() to create a new string, customGreeting, where “World” is replaced with “Pine Script”.
  • Finally, we display this custom greeting on the chart using label.new().

Key Features

  • Function Usability: str.replace_all() is a highly versatile function that can be used in a wide range of scenarios, from formatting text for display purposes to dynamically altering script parameters based on user input.
  • Syntax and Application: The function is straightforward, requiring only the source string, the target substring to replace, and the replacement string. This simplicity makes it easy to incorporate into your scripts without complicating your code.
  • Series Strings Support: The ability to process series strings makes str.replace_all() exceptionally useful in trading scripts where text labels or annotations might change based on series data.

Takeaways

  • The str.replace_all() function is essential for string manipulation in Pine Script, allowing for dynamic replacements within strings.
  • It supports both simple and series strings, making it adaptable to various scripting needs.
  • Its straightforward syntax and powerful functionality enable script developers to easily customize and enhance their trading indicators and strategies.

By mastering the str.replace_all() function, you can unlock new possibilities in your Pine Script development, from enhancing script readability to creating more interactive and user-friendly trading tools.

Leave a Comment