SPLIT-SEQUENCE
SPLIT-SEQUENCE (formerly known as PARTITION) is a member of the Common Lisp Utilities family of programs, designed by community consensus.

See also cl-ppcre:split.

Function SPLIT-SEQUENCE, SPLIT-SEQUENCE-IF, SPLIT-SEQUENCE-IF-NOT

Syntax:

split-sequence delimiter sequence &key count remove-empty-subseqs from-end start end test test-not key => list, index

split-sequence-if predicate sequence &key count remove-empty-subseqs from-end start end key => list, index

split-sequence-if-not predicate sequence &key count remove-empty-subseqs from-end start end key => list, index

Arguments and Values:

delimiter
an object.
predicate
a designator for a function of one argument that returns a generalized boolean.
sequence
a proper sequence.
count
an integer or nil. The default is nil.
remove-empty-subseqs
a generalized boolean. The default is false.
from-end
a generalized boolean. The default is false.
start, end
bounding index designators of sequence. The defaults for start and end are 0 and nil, respectively.
test
a designator for a function of two arguments that returns a generalized boolean.
test-not
a designator for a function of two arguments that returns a generalized boolean.
key
a designator for a function of one argument, or nil.
list
a proper sequence.
index
an integer greater than or equal to zero, and less than or equal to the length of the sequence.

Description:

Splits sequence into a list of subsequences delimited by objects satisfying the test.

List is a list of sequences of the same kind as sequence that has elements consisting of subsequences of sequence that were delimited in the argument by elements satisfying the test. Index is an index into sequence indicating the unprocessed region, suitable as an argument to subseq to continue processing in the same manner if desired.

The count argument, if supplied, limits the number of subsequences in the first return value; if more than count delimited subsequences exist in sequence, the count leftmost delimited subsequences will be in order in the first return value, and the second return value will be the index into sequence at which processing stopped.

If from-end is non-null, sequence is conceptually processed from right to left, accumulating the subsequences in reverse order; from-end only makes a difference in the case of a non-null count argument. In the presence of from-end, the count rightmost delimited subsequences will be in the order that they are in sequence in the first return value, and the secĀ­ond is the index indicating the end of the unprocessed region.

The start and end keyword arguments permit a certain subsequence of the sequence to be processed without the need for a copying stage; their use is conceptually equivalent to partitioning the subsequence delimited by start and end, only without the need for copying.

If remove-empty-subseqs is null (the default), then empty subsequences will be included in the result.

In all cases, the subsequences in the first return value will be in the order that they appeared in sequence.

It should be noted that in Common Lisp, strings are sequences and therefore split-sequence can also be used to split strings, and splitting strings is one of the most common use of split-sequence. Notably, newbies often ask about split-string or some other wait to split strings. Split-sequence is the answer to the Common Lisp split string quest.

Examples:

 (split-sequence:SPLIT-SEQUENCE #\Space "A stitch in time saves nine.")
=>  ("A" "stitch" "in" "time" "saves" "nine.")
    28
 (split-sequence:SPLIT-SEQUENCE #\, "foo,bar ,baz, foobar , barbaz,")
=>  ("foo" "bar " "baz" " foobar " " barbaz" "")
    30