Library · Pine Script v5 · Library

Strings

by ivansoft· code credited to PineCoders

A Pine Script v5 library of string helpers that add to the built-in string functions.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code69
Settings0inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago1 Mar 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

69 lines of Pine Script v5 from examples/lib/str_split.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

str_split.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PineCoders
//@version=5
 
// @description     This library provides string manipulation functions to complement the Pine Script™ `str.*()` built-in functions.
library("Strings", true)

// Strings Library
// v1, 2022.04.09 13:26
// This code was written using the recommendations from the Pine Script™ User Manual's Style Guide:
//   https://www.tradingview.com/pine-script-docs/en/v5/writing/Style_guide.html



// ———————————————————— Library functions {


// @function            Extracts the part of the `str` string that is left of the nth `occurrence` of the `separator` string.
// @param str           (series string) Source string.
// @param separator     (series string) Separator string.
// @param occurrence    (series int) Occurrence of the separator string. Optional. The default value is zero (the 1st occurrence).
// @returns             (string) The extracted string. 
export leftOf(simple string str, simple string separator, simple int occurrence = 0) =>
    str.match(str, "(?:[^" + separator + "]*" + separator + "){" + str.tostring(occurrence) + "}[^" + separator + "]*")
export leftOf(series string str, series string separator, series int occurrence = 0) =>
    str.match(str, "(?:[^" + separator + "]*" + separator + "){" + str.tostring(occurrence) + "}[^" + separator + "]*")


// @function            Extracts the part of the `str` string that is right of the nth `occurrence` of the `separator` string.
// @param str           (series string) Source string.
// @param separator     (series string) Separator string.
// @param occurrence    (series int) Occurrence of the separator string. Optional. The default value is zero (the 1st occurrence).
// @returns             (string) The extracted string. 
export rightOf(simple string str, simple string separator, simple int occurrence = 0) =>
    string match  = str.match(str, "([^" + separator + "]*" + separator + "\s*){" + str.tostring(occurrence + 1) + "}")
    string result = str.replace(str, match, "") 
export rightOf(series string str, series string separator, series int occurrence = 0) =>
    string match  = str.match(str, "([^" + separator + "]*" + separator + "\s*){" + str.tostring(occurrence + 1) + "}")
    string result = str.replace(str, match, "") 
// }



// ———————————————————— Example Code {


// Function to display one table cell.
printCell(id, col, row, str, clr) => 
    table.cell(id, col, row, str, text_color = clr, text_halign = text.align_left, text_size = size.normal)

// Determine if the last chart price is high, in which case we will position the table at the bottom.
bool lastPriceIsHigh = ta.percentrank(ohlc4, 100) > 50

// Display examples using a table.
if barstate.islast
    string tablePosition = lastPriceIsHigh ? position.bottom_right : position.top_right
    var table display = table.new(tablePosition, 2, 3, color.new(color.black, 100), color.gray, 1, color.gray, 1)
    color codeColor   = color.blue
    color resultColor = color.gray
    // Headers
    table.cell(display, 0, 0, "Code",   bgcolor = color.new(color.fuchsia, 60), text_color = color.white, text_halign = text.align_left, text_size = size.normal)
    table.cell(display, 1, 0, "Result", bgcolor = color.new(color.lime, 60),    text_color = color.white, text_halign = text.align_left, text_size = size.normal)
    // Left column
    printCell(display, 0, 1, 'leftOf("NASDAQ:MSFT", ":")\nleftOf("yyyy-mm-dd", "-", 2)', codeColor)
    printCell(display, 0, 2, 'rightOf("NASDAQ:MSFT", ":")\nrightOf("NASDAQ:MSFT, NASDAQ:TSLA", ":", 2)', codeColor)
    // Right column
    printCell(display, 1, 1, leftOf("NASDAQ:MSFT", ":") + "\n" + leftOf("yyyy-mm-dd", "-", 1), resultColor)
    printCell(display, 1, 2, rightOf("NASDAQ:MSFT", ":") + "\n" + rightOf("NASDAQ:MSFT, NASDAQ:TSLA", ":", 1), resultColor)
// }

Overview

Strings extracts the text to the left or right of the nth occurrence of a separator, with versions for simple and series strings. The file includes example code that shows the code and its result in a table on the chart, placed at the top or bottom depending on where price sits. It has no inputs and is imported by other scripts.

Details

Published by

ivansoft on GitHub

Code credit

PineCoders, as written in the script's header

Draws

Library (no plots)

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescripttradingvew

Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

261 stars

AIActivationFunctionsBipolarSigmoid is a Pine Script library that provides mathematical implementations of the bipolar sigmoid activation function and its derivatives for machine learning algorithms. It calculates bipolar sigmoid values bounded between -1 and

Library v5 AI / machine learning
Updated 4 years ago · None stated GitHub repository of AIActivationFunctionsBipolarSigmoid

KernelFunctions

by artificial-intelligence-edge
75 stars

KernelFunctions is a Pine Script v5 library that exports kernel weight functions for Nadaraya-Watson style estimation. It calculates a Rational Quadratic kernel, a Gaussian kernel based on a radial basis function, and a periodic kernel that models repeating be

Library v5
Updated 3 days ago · MIT GitHub repository of KernelFunctions

Ta

by nersent
29 stars

This Pine Script v5 library exports thin wrappers around built-in technical analysis functions. It covers moving averages, momentum measures such as RSI and CCI, volatility tools including ATR and standard deviation, range extremes, cross detection, and runnin

Library v5
Updated 11 months ago · Custom licence GitHub repository of Ta

Bands

by heyphat
26 stars

This library exports a Band type and functions that build a simple-moving-average basis with upper and lower envelopes from standard deviation. It classifies a source as above, below, or inside those envelopes, measures band width, and scales an imported z-sco

Library v6 Volatility & bandsMoving averages
Updated 2 months ago · AGPL-3.0 GitHub repository of Bands

Mylib

by ferranbt
21 stars

This script serves as a minimal utility library written in Pine Script v6. It provides an exported helper function that calculates the sum of two numeric arguments. As a reusable code library, it produces no visual plots, charts, or trading signals, and it con

Library v6
Updated 7 weeks ago · MPL-2.0 GitHub repository of Mylib

HLevel

by stupenkov
14 stars

HLevel is a Pine Script v5 utility library designed to manage horizontal price levels for cryptocurrency charts. It defines custom types for pivots and levels, along with methods to initialize, add, track, and expire price levels based on pivot high and pivot

Library v5
Updated 3 years ago · None stated GitHub repository of HLevel

More libraries

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers