field
field, function
def const pure field(haystack: text, separator: text, n: number): text
Returns the n
th field, zero-indexed from the left, in a text value haystack
that contains multiple sub-text values separated by a specified separator
.
This function is intended to facilitate parsing values that have been concatenated within a single table column.
Example
haystack = "a-b-c-d"
show summary "Example 1" with
field(haystack, "-", 0) // "a"
field(haystack, "-", 1) // "b"
field(haystack, "-", 2) // "c"
Remarks
Repeated separator
If the separator
is repeated twice in a row in the haystack
, an empty field is considered to separate them:
haystack2 = "a--c-d"
show summary "Example 2" with
field(haystack2, "-", 0) // "a"
field(haystack2, "-", 1) // ""
field(haystack2, "-", 2) // "c"
Out of range index
If the haystack
does not contain enough fields, an empty text is returned:
haystack3 = "a"
show summary "Example 3" with
field(haystack3, "-", 0) // "a"
field(haystack3, "-", 1) // ""
field(haystack3, "-", 2) // ""
Edge cases
If the separator
is empty, or if n
is negative, the function returns an empty text :
show summary "Edge cases" with
field("ab-cd", "", 1) // ""
field("ab-cd", "-", -1) // ""