PineCoders

Strings

█  OVERVIEW


This library provides string manipulation functions to complement the Pine Script™ `str.*()` built-in functions.



█  CONCEPTS


At the time our String Manipulation Framework was published, there was little in the way of built-in functions to manipulate strings. Since then, we have witnessed several meaningful developments on this front by the nimble Pine team. The newly released functions (including the ones in this blog post) have deprecated most of our functions. This library captures the small handful of functions we think are still pertinent. It is worth noting that, thanks to the new string built-ins in Pine Script™, these functions greatly outperform their earlier counterparts, both performance-wise and because they can return values of simple form, which are a necessity in some circumstances, such as when used as arguments to some parameters of request.security().



█  NOTES


`leftOf()` and `rightOf()`

Using the functions in this library is straightforward. The `leftOf()` and `rightOf()` functions extract the part of a string that is to the left or to the right of another string or character. This can be useful to separate the exchange and symbol components of user-entered tickers, for example. The separation is done with the underused str.match(), which can use regular expressions (or regex) to scan a string and separate characters based on a search pattern. The possibilities with regex are virtually endless; they can be used in “find and replace” applications, or to validate phone numbers, emails, passwords, credit card numbers, dates, etc. Note that Pine supports the same regex features as Java.


String operations in Pine Script™

The Pine Script™ runtime is optimized for number crunching. You can thus optimize script performance by limiting operations on strings whenever possible. This includes declaring strings with the var keyword, and containing re-assignments to local if blocks using barstate.islast, for example.


Look first. Then leap.



█  FUNCTIONS


leftOf(​str, separator, occurrence)
  Extracts the part of the `str` string that is left of the nth `occurrence` of the `separator` string.
  Parameters:
    str: (series string) Source string.
    separator: (series string) Separator string.
    occurrence: (series int) Occurrence of the separator string. Optional. The default value is zero (the 1st occurrence).
  Returns: (string) The extracted string.

rightOf(​str, separator, occurrence)
  Extracts the part of the `str` string that is right of the nth `occurrence` of the `separator` string.
  Parameters:
    str: (series string) Source string.
    separator: (series string) Separator string.
    occurrence: (series int) Occurrence of the separator string. Optional. The default value is zero (the 1st occurrence).
  Returns: (string) The extracted string.

Tools and ideas for all Pine coders: www.pinecoders.com
Our Pine FAQ & Code: www.pinecoders.com/faq_and_code/
Pine news broadcasts: t.me/PineCodersSquawkBox or twitter.com/PineCoders
Bibliothèque Pine

Dans le véritable esprit de TradingView, l'auteur a publié ce code Pine en tant que bibliothèque open-source afin que d'autres programmeurs Pine de notre communauté puissent le réutiliser. Bravo à l'auteur ! Vous pouvez utiliser cette bibliothèque à titre privé ou dans d'autres publications open-source, mais la réutilisation de ce code dans une publication est régie par notre Règlement.

Clause de non-responsabilité

Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.

Vous voulez utiliser cette bibliothèque?

Copiez le texte dans le presse-papiers et collez-le dans votre script.