Strings
Formatting, conversion, searching, and manipulation of strings.
All string functions are available under the str.* namespace.
Conversion and Formatting
str.toString
str.toString(value, format?) -> stringConverts a value to a string. When value is a number, an optional format controls output.
| Param | Type | Default | Description |
|---|---|---|---|
value | any | — | Value to convert |
format | string | — | Number format preset or pattern |
Format presets:
| Preset | Alias | Description |
|---|---|---|
"mintick" | "format.mintick" | Rounds to the symbol's tick size |
"percent" | "format.percent" | Formats as percentage |
"volume" | "format.volume" | Compact volume notation (K, M, B) |
You can also pass a custom pattern like "#.00" or "0,000.##".
str.toString(123.456, "mintick") // "123.46" (depends on tick size)
str.toString(0.0534, "percent") // "5.34%"
str.toString(1500000, "volume") // "1.5M"
str.toString(3.14159, "#.00") // "3.14"str.format
str.format(template, ...args) -> stringFills placeholders in a template string. Placeholders use {index} syntax with optional numeric formatting.
| Param | Type | Description |
|---|---|---|
template | string | Template with {0}, {1} placeholders |
...args | any | Values to insert |
str.format("Price: {0}", 42000)
// "Price: 42000"
str.format("{0,number,#.00} / {1,number,#.0}", 3.14159, 99.5)
// "3.14 / 99.5"Info
str.length
str.length(source) -> numberReturns the number of characters in a string.
str.length("hello") // 5str.includes
str.includes(source, sub) -> booleanReturns true if source contains sub.
str.includes("hello world", "world") // true
str.includes("hello world", "xyz") // falsestr.startsWith
str.startsWith(source, sub) -> booleanReturns true if source starts with sub.
str.startsWith("binancef", "binance") // truestr.endsWith
str.endsWith(source, sub) -> booleanReturns true if source ends with sub.
str.endsWith("btc/usdt", "usdt") // truestr.indexOf
str.indexOf(source, sub, occurrence?) -> numberReturns the index of the occurrence-th match of sub in source, or -1 if not found.
| Param | Type | Default | Description |
|---|---|---|---|
source | string | — | String to search in |
sub | string | — | Substring to find |
occurrence | number | 0 | Which occurrence (0 = first) |
str.indexOf("a.b.c", ".") // 1
str.indexOf("a.b.c", ".", 1) // 3
str.indexOf("abc", "z") // -1Manipulation
str.split
str.split(source, separator) -> string[]Splits a string into an array by separator.
str.split("a,b,c", ",") // ["a", "b", "c"]str.replace
str.replace(source, target, replacement, occurrence?) -> stringReplaces the occurrence-th match of target with replacement.
| Param | Type | Default | Description |
|---|---|---|---|
source | string | — | Input string |
target | string | — | Substring to find |
replacement | string | — | Replacement text |
occurrence | number | 0 | Which occurrence to replace (0 = first) |
str.replace("foo bar foo", "foo", "baz") // "baz bar foo"
str.replace("foo bar foo", "foo", "baz", 1) // "foo bar baz"str.replaceAll
str.replaceAll(source, target, replacement) -> stringReplaces all occurrences of target.
str.replaceAll("foo bar foo", "foo", "baz") // "baz bar baz"str.substring
str.substring(source, begin, end?) -> stringExtracts a portion of the string from begin to end (exclusive). If end is omitted, extracts to the end.
| Param | Type | Default | Description |
|---|---|---|---|
source | string | — | Input string |
begin | number | — | Start index (inclusive) |
end | number | end of string | End index (exclusive) |
str.substring("hello", 1, 3) // "el"
str.substring("hello", 2) // "llo"str.toUpperCase
str.toUpperCase(source) -> stringConverts to uppercase.
str.toUpperCase("hello") // "HELLO"str.toLowerCase
str.toLowerCase(source) -> stringConverts to lowercase.
str.toLowerCase("HELLO") // "hello"str.trim
str.trim(source) -> stringRemoves leading and trailing whitespace.
str.trim(" hello ") // "hello"str.repeat
str.repeat(source, count, separator?) -> stringRepeats a string count times with an optional separator between repetitions.
| Param | Type | Default | Description |
|---|---|---|---|
source | string | — | String to repeat |
count | number | — | Number of repetitions |
separator | string | "" | Separator between repetitions |
str.repeat("ab", 3) // "ababab"
str.repeat("ab", 3, "-") // "ab-ab-ab"