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
مكتبة باين

كمثال للقيم التي تتبناها TradingView، نشر المؤلف شيفرة باين كمكتبة مفتوحة المصدر بحيث يمكن لمبرمجي باين الآخرين من مجتمعنا استخدامه بحرية. تحياتنا للمؤلف! يمكنك استخدام هذه المكتبة بشكل خاص أو في منشورات أخرى مفتوحة المصدر، ولكن إعادة استخدام هذا الرمز في المنشور تحكمه قواعد الموقع.

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.

هل تريد استخدام هذه المكتبة؟

انسخ النص إلى الحافظة وألصقه في النص البرمجي الخاص بك.