Re: Newbie lisp problem: string to list of strings separated by space



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
.



Relevant Pages

  • Re: Clearing Variables
    ... I am busy with VB6 you should have seen it, it seems that I remember much but not all, it has cost me yesterday almost two hour to remember what was the code to find something in a string. ... Public Sub ClearLine(ByVal LineNumber As Long) ... Dim pos As Long ... FieldLine = RTrim$(Mid$(PrintData, pos, FieldSize)) ...
    (microsoft.public.vb.general.discussion)
  • Re: Open Sound Control
    ... \0s at the end of the string to pad it to a 32bit boundary. ... puts [string length $packet] ... instead it extracts both the I* and R* lists once ... set len [lindex $l $pos] ...
    (comp.lang.tcl)
  • FAQ 6.19 What good is "G" in a regular expression?
    ... any characters to find the next match with this anchor, ... It uses the value of pos() as the ... Each string has its own posvalue. ... After the match fails at the letter "a", perl resets pos() and the next ...
    (comp.lang.perl.misc)
  • FAQ 6.19 What good is "G" in a regular expression?
    ... any characters to find the next match with this anchor, ... It uses the value of pos() as the ... Each string has its own posvalue. ... After the match fails at the letter "a", perl resets pos() and the next ...
    (comp.lang.perl.misc)
  • FAQ 6.19 What good is "G" in a regular expression?
    ... any characters to find the next match with this anchor, ... It uses the value of pos() as the ... Each string has its own posvalue. ... After the match fails at the letter "a", perl resets pos() and the next ...
    (comp.lang.perl.misc)