@explode(s:string)
@explode(s:string, t:string)
@explode(s:string, t:tab of string)
@explode(s:string, t:tab of int)
@explode(s:string, t:funct)
Function @explode is used to split a string into a
sequence of contiguous substrings.
The first form (one argument) returns a tab containing the characters of
s (the characters are represented as string with only one
element). For example:
@explode("") -> []
@explode("abc") -> ["a", "b", "c"]
@reduce(@+, @explode("abc")) -> "abc"
@scan(@+, @explode("abc")) -> ["a", "ab", "abc"]
The others forms (two arguments) use the second argument as a
specification of delimiters in the string s and split the string
accordingly in a sequence of substrings:
-
if
tis a string, the letter of the string are used as a delimiter, -
if
tis a tab of strings, these strings are merged and the corresponding letters are used as delimiters, -
if
tis a tab of integers, these integers are used as indices given the positions of teh delimiters (the tab must defines a sequence of strictly increasing indices), -
if
tis a predicate (an unary function returning a value interpreted as a boolean: map, function, lambda, etc.), the predicate is applied to each letter ofsto determine the delimiters.
The string s is split in a sequence of substrings that are a partition
of the first argument, i.e. the substring are non-overlaping,
contiguous and non-empty. Each substring begins by a delimiter and ends
before a delimiter, except for the first substring which is a prefix of
s that ends just before the first delimiters and the last substrings
that end with the end of s. If the resulting substring is empty, it is
removed from the result.
For instance :
"".explode(["b"]) -> []
"a".explode(["b"]) -> ["a"]
"ab".explode(["b"]) -> ["a", "b"]
"abaa".explode(["b"]) -> ["a", "baa"]
"abbb".explode(["b"]) -> ["a", "b", "b", "b"]
@fun_def compar($x, $y) { return $x == $y }
$a := @compar("a")
"".explode($a) -> []
"a".explode($a) -> ["a"]
"ab".explode($a) -> ["ab"]
"abb".explode($a) -> ["abb"]
"abba".explode($a) -> ["abb", "a"]
"abcd".explode([0, 0]) -> ["abcd"]
"abcd".explode([0, 1]) -> ["a", "bcd"]
"abcd".explode([1, 2]) -> ["a", "b", "cd"]
"abcd".explode([2, 2, 2]) -> ["ab", "cd"]
Remarks that @explode(x) == @explode(x, \$a.(true)).
See also String Management @car @cdr @char_is_alnum @char_is_alpha @char_is_ascii @char_is_blank @char_is_cntrl @char_is_digit @char_is_graph @char_is_lower @char_is_print @char_is_punct @char_is_space @char_is_upper @char_is_xdigit @copy @count @drop @dump @dumpvar @empty @explode @find @is_prefix @is_string @is_subsequence @is_suffix @last @member @occurs @parse @permute @push_back @r_compile @r_findall @r_match @r_search @remove @remove_duplicate @replace @scramble @slice @sort @split @sputter @string2fun @string2proc @strip_path @stutter @system @take @to_num @Tracing @UnTracing
Most functions acting on tabs operate also on strings.