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
Pineライブラリ

TradingViewの精神に則り、作者はPineコードをオープンソースライブラリとして公開し、コミュニティの他のPineプログラマーが再利用できるようにしました。作者に敬意を表します!このライブラリを個人的に、または他のオープンソースの投稿で使用することができますが、、このコードを投稿で再利用するには、ハウスルールに準拠する必要があります。

免責事項

これらの情報および投稿は、TradingViewが提供または保証する金融、投資、取引、またはその他の種類のアドバイスや推奨を意図したものではなく、またそのようなものでもありません。詳しくは利用規約をご覧ください。

このライブラリを使用したいですか?

以下の行をコピーして、スクリプト内に貼り付けてください。