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])Description
Section titled “Description”The slice function takes a string or list as input and selects parts from it.
x: string|list
Section titled “x: string|list”The string or list to slice.
begin = int (optional)
Section titled “begin = int (optional)”The offset to start slice from.
If negative, offset is calculated from the end of the input.
Defaults to 0.
end = int (optional)
Section titled “end = int (optional)”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.
stride = int (optional)
Section titled “stride = int (optional)”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.
Examples
Section titled “Examples”Get the first 3 characters of a string
Section titled “Get the first 3 characters of a string”from {x: "123456789"}x = x.slice(end=3){x: "123"}Get the 1st, 3rd, and 5th characters
Section titled “Get the 1st, 3rd, and 5th characters”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"}Get the first 3 elements of a list
Section titled “Get the first 3 elements of a list”from {xs: [1, 2, 3, 4, 5]}xs = xs.slice(end=3){xs: [1, 2, 3]}Slice a list with a negative begin offset
Section titled “Slice a list with a negative begin offset”from {xs: [1, 2, 3, 4, 5]}xs = xs.slice(begin=-3){xs: [3, 4, 5]}Reverse a list
Section titled “Reverse a list”from {xs: [1, 2, 3, 4, 5]}xs = xs.slice(stride=-1){xs: [5, 4, 3, 2, 1]}