Idiom # 259: Split on several separators
See programming-idioms.org :
Code
Running Example
Using string manipulation
Code input
ex259 is
strings := ["a-b_c,d", "-a--cc_,eee,ffff_", "---", ",_-", "abcd", ""]
for s in strings do
parts := s.replace "_" ","
.replace "-" ","
.split ","
say "\"$s\" : $parts"
Alternative, more elegant, approach using String.fields_func
Note that this treats multiple neighboring seperators as one, so no empty strings are added in that case.
Code input
ex259 is
strings := ["a-b_c,d", "-a--cc_,eee,ffff_", "---", ",_-", "abcd", ""]
seperators := container.set_of_ordered [",", "-", "_"]
for s in strings do
parts := s.fields_func (∈ seperators)
say "\"$s\" : $parts"
last changed: 2025-05-13