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
Pinebibliotek

I sann TradingView-anda har författaren publicerat denna Pine-kod som ett bibliotek med öppen källkod så att andra Pine-programmerare från vår community kan återanvända den. Hatten av för författaren! Du kan använda det här biblioteket privat eller i andra publikationer med öppen källkod, men återanvändning av den här koden i en publikation regleras av våra ordningsregler.

Frånsägelse av ansvar

Informationen och publikationerna är inte avsedda att vara, och utgör inte heller finansiella, investerings-, handels- eller andra typer av råd eller rekommendationer som tillhandahålls eller stöds av TradingView. Läs mer i Användarvillkoren.

Vill du använda det här biblioteket?

Kopiera följande rad och klistra in det i ditt skript.