Multisplit: Difference between revisions

Content added Content deleted
(add SenseTalk examples)
m (clean up to make examples clearer)
Line 1,732: Line 1,732:


=={{header|SenseTalk}}==
=={{header|SenseTalk}}==
First approach, using line delimiters. Lines are delimited by an array of separator strings, normally [CRLF, LF, CR, lineSeparator(0x2028), paragraphSeparator(0x2029)]. Supplying an alternate set of delimiters lets us split a string by a different (ordered) set of strings:
<lang sensetalk>

set source to "a!===b=!=c"
<lang sensetalk>set source to "a!===b=!=c"
set separators to ["==", "!=", "="]
set separators to ["==", "!=", "="]


put each line delimited by separators of source</lang>
// first approach, using line delimiters
Output:
put each line delimited by separators of source
<lang sensetalk>(a,,b,,c)</lang>


Second approach, using a pattern. SenseTalk's pattern language lets us define a pattern (a regex) which can then be used to split the string and also to display the actual separators that were found.
// second approach, using a pattern
set source to "a!===b=!=c"
<lang sensetalk>set source to "a!===b=!=c"
set separatorPattern to <"==" or "!=" or "=">
set separatorPattern to <"==" or "!=" or "=">

put source split by separatorPattern
put source split by separatorPattern


// and show the separators that were found, for extra credit:
put each occurrence of separatorPattern in source
put each occurrence of separatorPattern in source
</lang>
</lang>
Output:
Output:
<lang sensetalk>(a,,b,,c)
<lang sensetalk>(a,,b,,c)
(a,,b,,c)
(!=,==,=,!=)</lang>
(!=,==,=,!=)</lang>
Explanation:
Lines are delimited by an array of separator strings, normally [CRLF, LF, CR, lineSeparator(0x2028), paragraphSeparator(0x2029)]. Supplying an alternate set of delimiters lets us split a string by a different (ordered) set of strings.

SenseTalk's pattern language lets us define a pattern (a regex) which can then be used to split the string and also to display the actual separators that were found.



=={{header|Sidef}}==
=={{header|Sidef}}==