String Manipulation Framework [PineCoders FAQ]
"String Manipulation Framework [PineCoders FAQ]" by PineCoders, MPL-2.0. Ported to FloScript by FloSignals; modifications under MPL-2.0. Original: String Manipulation Framework [PineCoders FAQ] (v1.0), read 2026-11-15T18:49:47.573Z; source language v4.
Licence: Mozilla Public License 2.0 (MPL-2.0) — full text. The source is published with it, modifications included, under the same licence.
String Manipulation Framework [PineCoders FAQ] by PineCoders, ported from Pine v4. Pane indicator with drawings and 6 inputs.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (Notice added by FloSignals: the author's source carries none. It was published open-source on 2021-01-22; since 2019-07-26 the platform it was published on has released such a script under MPL-2.0 automatically.)
// REUSING THIS CODE: You are welcome to reuse this code without permission, including in closed-source publications. Credits are appreciated.
//@author=LucF, for PineCoders
// PineCoders, Tools and ideas for all Pine coders: pinecoders.com
// String Manipulation Framework - PineCoders FAQ
// v1.0, 2021.01.22 10:11
// This script contains functions to help coders manipulate strings.
// This code was written using:
// • PineCoders Coding Conventions for Pine: http://www.pinecoders.com/coding_conventions/
// PineCoders FAQ & Code question linking here: https://www.pinecoders.com/faq_and_code/#how-can-i-draw-lines-or-labels-into-the-future
// ------------------------------------------------------------------------------------------------
// Original author: PineCoders
// Original title: String Manipulation Framework [PineCoders FAQ]
// Licence: MPL-2.0 (the notice above was added by FloSignals; the original carries none)
// Pine version: 4
// Fetched: 2026-11-15
// Ported to FloScript by FloSignals; modifications under MPL-2.0.
// Converter: @flosignals/floscript 0.1.0, strict mode, score 100/100
// ------------------------------------------------------------------------------------------------
//@floscript=1
//@version=5
// Converted by the FloScript converter from a Pine-compatible v4 source.
indicator("String Manipulation Framework [PineCoders FAQ]")
// ———————————————————— Inputs {
i_fmt = input("-????0.0", "Format string used in Columns 1-3")
i_v1 = input(-123.456)
i_v2 = input(789.4)
i_v3 = input(-2)
i_v4 = input(34.123456789)
i_srcStr = input("AB1cd:1A23Z", "Source string used in Column 4")
// }
// ———————————————————— Functions {
// ————— Function returning the leftmost `_n` characters in `_str`.
f_strLeft(_str, _n) =>
// string _str: source string.
// int _n : number of leftmost characters to return.
array<string> _chars = str.split(_str, "")
int _len = array.size(_chars)
int _end = math.min(_len, math.max(0, _n))
array<string> _substr = array.new_string(0)
if _end <= _len
_substr := array.slice(_chars, 0, _end)
string _return = array.join(_substr, "")
// ————— Function returning the rightmost `_n` characters in `_str`.
f_strRight(_str, _n) =>
// string _str: source string.
// int _n : number of rightmost characters to return.
array<string> _chars = str.split(_str, "")
int _len = array.size(_chars)
int _beg = math.max(0, _len - _n)
array<string> _substr = array.new_string(0)
if _beg < _len
_substr := array.slice(_chars, _beg, _len)
string _return = array.join(_substr, "")
// ————— Function returning the substring of `_str` from character position `_from` to `_to` inclusively.
f_strMid(_str, _from, _to) =>
// string _str : source string.
// int _from: left character position. The first character's position is 0.
// int _to : right character position.
array<string> _chars = str.split(_str, "")
int _len = array.size(_chars)
int _beg = math.min(_len - 1, math.max(0, _from))
int _end = math.min(_len, math.max(0, _to + 1))
array<string> _substr = array.new_string(0)
if _beg <= _end
_substr := array.slice(_chars, _beg, _end)
string _return = array.join(_substr, "")
// ————— Function returning the sub-string of `_str` to the left of the `_of` separating character.
f_strLeftOf(_str, _of) =>
// string _str: string to separate.
// string _op : separator character.
array<string> _chars = str.split(_str, "")
int _len = array.size(_chars)
int _ofPos = array.indexof(_chars, _of)
array<string> _substr = array.new_string(0)
if _ofPos > 0 and _ofPos <= _len - 1
_substr := array.slice(_chars, 0, _ofPos)
string _return = array.join(_substr, "")
// ————— Function returning the sub-string of `_str` to the right of the `_of` separating character.
f_strRightOf(_str, _of) =>
// string _str: string to separate.
// string _op : separator character.
array<string> _chars = str.split(_str, "")
int _len = array.size(_chars)
int _ofPos = array.indexof(_chars, _of)
array<string> _substr = array.new_string(0)
if _ofPos >= 0 and _ofPos < _len - 1
_substr := array.slice(_chars, _ofPos + 1, _len)
string _return = array.join(_substr, "")
// ————— Function returning the position of the first occurrence of `_chr` in `_str`, where the first character position is 0. Returns -1 if the character is not found.
f_strCharPos(_str, _chr) =>
// string _str: string to search.
// string _chr: character to search for in `_str`.
array<string> _chars = str.split(_str, "")
int _len = array.size(_chars)
int _return = array.indexof(_chars, _chr)
// ————— Function that replaces a character at position `_pos` in the `_src` string with the `_str` character or string.
f_strReplace(_src, _pos, _str) => // Thx to @LonesomeTheBlue.
// string _src : source string.
// int _pos : position of character to be replaced. The first character's position is 0.
// string _str : replacement character or string.
array<string> _chars = str.split(_src, "")
int _len = array.size(_chars)
if _pos >= 0 and _pos < _len
array.set(_chars, _pos, _str)
string _return = array.join(_chars, "")
// ————— Function returning a format string usable with `tostring()` to round a value to the symbol's tick precision.
f_tickFormat() =>
_s = str.tostring(syminfo.mintick)
_s := str.replace_all(_s, "25", "00")
_s := str.replace_all(_s, "5", "0")
_s := str.replace_all(_s, "1", "0")
// }
// ————— Function returning a string representation of a numeric `_val` using a special `_fmt` string allowing all strings to be of the same width, to help align columns of values.
f_tostringPad(_val, _fmt) =>
// float _val: string to separate.
// string _fmt: formatting string similar to those used in the `tostring()` format string, with "?" used to indicate padding,
// e.g., "??0.00" to ensure that numbers always contain three characters left of the decimal point.
// WARNING: A minus sign MUST begin the format ("-??0.00") if negative values are to be handled, otherwise negative values will have no minus sign.
// Character used to pad numbers: Unicode figure space (U+2007).
var string FIGURE_SPACE = " "
// Character used to pad minus signs: Unicode punctuation space (U+2008).
var string PUNCTUATION_SPACE = " "
// Preliminary conversion of the value to a string.
string _formattedVal = str.tostring(_val, _fmt)
// Remove minus sign from format string.
_formattedVal := str.replace_all(_formattedVal, "-", "")
// Split value and format string into characters.
array<string> _chars = str.split(_formattedVal, "")
array<string> _charsFmt = str.split(_fmt, "")
// Detect if we need to handle negative values, and if so, remove the minus sign from format.
bool _handleNegs = array.get(_charsFmt, 0) == "-"
if _handleNegs
array.shift(_charsFmt)
// Detect if value or format string are without a decimal point and if so, add a temporary one for our logic, which we'll remove later.
int _pointPos = array.indexof(_chars, ".")
int _pointPosFmt = array.indexof(_charsFmt, ".")
bool _noPoint = _pointPos == -1
bool _noPointFmt = _pointPosFmt == -1
if _noPoint
array.push(_chars, ".")
_pointPos := array.indexof(_chars, ".")
if _noPointFmt
array.push(_charsFmt, ".")
_pointPosFmt := array.indexof(_charsFmt, ".")
// Remove extra instances of "?" in front of value.
_extraChars = _pointPos - _pointPosFmt
if _extraChars > 0
for _i = 0 to _extraChars - 1
array.shift(_chars)
// Add minus sign or space if needed.
if _handleNegs
// Format accounts for negative values; insert minus sign or space.
array.unshift(_chars, _val < 0 ? "-" : PUNCTUATION_SPACE)
// Remove temporary decimal point if needed.
if _noPointFmt or _noPoint
array.pop(_chars)
// Rejoin our array and replace the "?" chars left in our result with figure space padding.
string _return = array.join(_chars, "")
_return := str.replace_all(_return, "?", FIGURE_SPACE)
// ————— Function prints a label on dataset's last bar.
f_print(_txt, _y, _color, _offsetLabels, _align) =>
// Keep track of minimum time between bars (chart's timeframe in ms) using `abs()` so this works with pre-1970 negative times.
var _tf = 10e15
_tf := math.min(math.abs(time - nz(time[1])), _tf)
// Calculate time offset for x position.
_t = int(time + _tf * _offsetLabels)
// Create the label on the first bar and only update it when on the dataset's last bar; it's more efficient.
var label _lbl = label.new(_t, _y, _txt, xloc.bar_time, yloc.price, #00000000, label.style_label_center, _color, size.large, _align)
if barstate.islast
label.set_xy(_lbl, _t, _y)
label.set_text(_lbl, _txt)
label.set_textcolor(_lbl, _color)
label.set_textalign(_lbl, _align)
// ———————————————————— Plots {
// ————— Column 0 (red): No format.
f_print("Column 0\nNo format:\n" + "[" + str.tostring(i_v1) + "]\n" + "[" + str.tostring(i_v2) + "]\n" + "[" + str.tostring(i_v3) + "]\n" + "[" + str.tostring(i_v4) + "]\n", 0, color.red, -225, text.align_center)
// ————— Column 1 (green): Displays values using the format string.
f_print("Column 1\nFormat as is:\n" + "[" + f_tostringPad(i_v1, i_fmt) + "]\n" + "[" + f_tostringPad(i_v2, i_fmt) + "]\n" + "[" + f_tostringPad(i_v3, i_fmt) + "]\n" + "[" + f_tostringPad(i_v4, i_fmt) + "]\n", 0, color.green, -190, text.align_center)
// ————— Column 2 (aqua): Displays integers using left part of format string only.
var leftPartOfFormat = f_strCharPos(i_fmt, ".") >= 0 ? f_strLeftOf(i_fmt, ".") : i_fmt
f_print("Column 2\nLeft part of format:\n" + "[" + f_tostringPad(i_v1, leftPartOfFormat) + "]\n" + "[" + f_tostringPad(i_v2, leftPartOfFormat) + "]\n" + "[" + f_tostringPad(i_v3, leftPartOfFormat) + "]\n" + "[" + f_tostringPad(i_v4, leftPartOfFormat) + "]\n", 0, color.aqua, -155, text.align_center)
// ————— Column 3 (fuchsia): Displays values using tick precision.
var tickPrecision = leftPartOfFormat + "." + f_strRightOf(f_tickFormat(), ".")
f_print("Column 3\nLeft part + tick precision:\n" + "[" + f_tostringPad(i_v1, tickPrecision) + "]\n" + "[" + f_tostringPad(i_v2, tickPrecision) + "]\n" + "[" + f_tostringPad(i_v3, tickPrecision) + "]\n" + "[" + f_tostringPad(i_v4, tickPrecision) + "]\n", 0, color.fuchsia, -105, text.align_center)
// —————————— Column 4 (blue): Two-column table showing string functions results.
// ————— Print column A of table.
f_print('Column 4A\ni_srcStr' + "\n\n" + 'f_strLeft(i_srcStr, 2)' + "\n" + 'f_strRight(i_srcStr, 1)' + "\n" + 'f_strMid(i_srcStr, 3, 5)' + "\n" + 'f_strLeftOf(i_srcStr, ":")' + "\n" + 'f_strRightOf(i_srcStr, ":")' + "\n" + 'f_strCharPos(i_srcStr, ":")' + "\n" + 'f_strReplace(i_srcStr, 2, "22")' + "\n" + "", 0, color.blue, -50, text.align_left)
// ————— Overprint column B of table using padding spaces for alignment.
// These are Em spaces (U+2003). Their qty will depend on the width of the widest text we print in column A.
var padding = " "
f_print(padding + "Column 4B" + "\n" + padding + " = [" + i_srcStr + "]\n\n" + padding + " = [" + f_strLeft(i_srcStr, 2) + "]\n" + padding + " = [" + f_strRight(i_srcStr, 1) + "]\n" + padding + " = [" + f_strMid(i_srcStr, 3, 5) + "]\n" + padding + " = [" + f_strLeftOf(i_srcStr, ":") + "]\n" + padding + " = [" + f_strRightOf(i_srcStr, ":") + "]\n" + padding + " = " + str.tostring(f_strCharPos(i_srcStr, ":")) + "\n" + padding + " = [" + f_strReplace(i_srcStr, 2, "22") + "]\n" + "", 0, color.blue, -50, text.align_left)
// }
This file is under the Mozilla Public License 2.0; it is a port of the original by PineCoders, with the original licence header kept and the changes made for FloScript noted in the file.
- v1
Library refresh 2026-11-15
Disclaimer. Scripts are written and published by members, or ported by FloSignals from open-source originals, and are their authors' own work and opinion. FloSignals does not review them for correctness and nothing in a script is financial advice or a recommendation to buy or sell anything — read the Risk Disclaimer. Posting here is under the Community Rules; a script that is not the poster's to post can be reported as the Copyright & Takedowns page says.

Comments (0)
No comments yet. Members can leave one.