Skip to content

Slices a string or list with offsets and strides.

slice(x:string, [begin=int, end=int, stride=int])
slice(x:list, [begin=int, end=int, stride=int])

The slice function takes a string or list as input and selects parts from it.

The string or list to slice.

The offset to start slice from.

If negative, offset is calculated from the end of the input.

Defaults to 0.

The offset to end the slice at.

If negative, offset is calculated from the end of the input.

If unspecified, ends at the input’s end.

The step size between elements to select.

For strings, stride must be positive. For lists, stride can be negative. When negative, elements are selected in reverse order from the range defined by begin and end.

Defaults to 1.

from {x: "123456789"}
x = x.slice(end=3)
{x: "123"}
from {x: "1234567890"}
x = x.slice(stride=2, end=6)
{x: "135"}

Select a substring from the 2nd character up to the 8th character

Section titled “Select a substring from the 2nd character up to the 8th character”
from {x: "1234567890"}
x = x.slice(begin=1, end=8)
{x: "2345678"}
from {xs: [1, 2, 3, 4, 5]}
xs = xs.slice(end=3)
{xs: [1, 2, 3]}
from {xs: [1, 2, 3, 4, 5]}
xs = xs.slice(begin=-3)
{xs: [3, 4, 5]}
from {xs: [1, 2, 3, 4, 5]}
xs = xs.slice(stride=-1)
{xs: [5, 4, 3, 2, 1]}

Last updated: