Skip to content

@lang:str.split

Split a string by a delimiter into an array of strings, or select one segment when an index is given.

On-chain (@lang:str.split!): The segment index is required; the delimiter may be a live call, which costs three reads of it per segment.

Returns: array | string

@lang:str.split(s delim index?)
NameTypeDescription
sstringSource string
delimstringExact, non-empty delimiter byte sequence
[index]numberSegment to select instead of the whole array: zero-based from the start, or negative from the end (-1 = last, -2 = second-last, and so on)

Split the string return of a call on a delimiter and select one segment, on-chain. Segment indexes are 0, 1, 2, … from the start, or -1, -2, … from the end (-1 is the last segment).

load lang
set $pool 0x44fA8E6f47987339850636F88629646662444217
# "Uniswap LP Token" -> segment 1 is "LP"
assert @str.split!($pool::{name()(string)} " " 1) == "LP"
# The name ends with "Token": negative index counts from the end, on-chain
assert @str.split!($pool::{name()(string)} " " -1) == "Token"
  • A live delimiter costs three reads of it per segment: the two indexOf bounds and its own length. Index 0 is the exception, needing neither the length nor a second indexOf.
  • Splits on the exact byte sequence; adjacent delimiters produce empty segments and an out-of-range index (in either direction) reverts with SegmentIndexOutOfBounds.
  • Negative indices resolve against the segment count at assertion time, so -1 is the last segment however many the live value has.
  • String-valued: compare with ==/!=. At the top level the core judges the string directly; nested inside @bool! the comparison compiles to an on-chain keccak comparison of the two sides.
  • assert, @hash!