Re: Newbie lisp problem: string to list of strings separated by space
- From: John Landahl <john@xxxxxxxxxxx>
- Date: Thu, 30 Mar 2006 08:54:55 -0800
Yossarian <yossarian@xxxxxxxxxxx> writes:
I am trying to write a very simple function that accepts a string as
input, and that returns a list of strings as output, having a
seperate list item for every substring without a space.
It seems we all write one of these eventually. Here's one I came up
with a while back:
(defun string-split (split-string string)
"Returns a list containing items in 'string' split from
occurrences of 'split-string'."
(loop with l = (length split-string)
for n = 0 then (+ pos l)
for pos = (search split-string string :start2 n)
if pos collect (subseq string n pos)
else collect (subseq string n)
while pos))
CL-USER> (string-split " " "yeah yeah yeah")
("yeah" "yeah" "yeah")
Also splits on strings of any length:
CL-USER> (string-split "..." "yes...yes...yes")
("yes" "yes" "yes")
But then you're much better off getting CL-PPCRE and using its SPLIT
function, which is far more powerful:
CL-USER> (cl-ppcre:split "\\s" "yeah yeah yeah")
("yeah" "yeah" "yeah")
CL-USER> (cl-ppcre:split "[,:;]" "hello,there:sir")
("hello" "there" "sir")
--
John Landahl <john@xxxxxxxxxxx>
http://landahl.org/john
.
- References:
- Prev by Date: Re: What language will be used to write the first self aware program?
- Next by Date: Re: (make-pathname :name "/root") - good/bad?
- Previous by thread: Re: Newbie lisp problem: string to list of strings separated by space
- Next by thread: Re: Newbie lisp problem: string to list of strings separated by space
- Index(es):
Relevant Pages
|