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 ผู้เขียนได้ตีพิมพ์รหัส Pine นี้เป็นไลบรารีโอเพนซอร์ซเพื่อให้โปรแกรมเมอร์คนอื่น ๆ จากชุมชนของเราสามารถนำไปใช้ซ้ำได้ ไชโยให้กับผู้เขียน! คุณสามารถใช้ไลบรารีนี้เป็นการส่วนตัวหรือในสิ่งพิมพ์โอเพนซอร์สอื่น ๆ แต่นำรหัสนี้มาใช้ซ้ำในสิ่งพิมพ์จะถูกควบคุมโดย กฎระเบียบการใช้งาน

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ เงื่อนไขการใช้บริการ

ต้องการใช้ไลบรารีนี้หรือไม่?

คัดลอกข้อความไปยังคลิปบอร์ดแล้ววางลงในสคริปต์ของคุณ