Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Splitting a string on a character...

19 views
Skip to first unread message

WJ

unread,
Nov 14, 2012, 10:45:08 PM11/14/12
to
Thomas F. Burdick wrote:

> Cory Spencer <cspe...@interchange.ubc.ca> writes:
>
> > Just a quickie question - is there already a Common Lisp function that
> > will split a string on a given character?
>
> No. Personally, I'm glad. All the functions that deal with sequences
> let you specify :start and :end, and we have POSITION. Which all add
> up to a more reasonable, less garbage-y way of doing things. You can
> see more of my thoughts on the matter here:

Wow! You mean that CL is so powerful that it enables one to roll
his own split function? Gee whiz! It's like a Turing Machine or
assembly language! No wonder you're glad! I wish I had a language
like that: one that makes you continually reinvent the wheel!



Harald Hanche-Olsen wrote:

> (defun split (thing sequence)
> (loop for start = 0 then (1+ end)
> for end = (position thing sequence :start start)
> collect (subseq sequence start end)
> while end))


Clojure:


user=> (clojure.string/split "eeny,meeny , miney, mo" #"[, ]+")
["eeny" "meeny" "miney" "mo"]


Say we want to stop splitting after we have 3 items:

user=> (clojure.string/split "eeny,meeny , miney, mo" #"[, ]+" 3)
["eeny" "meeny" "miney, mo"]
0 new messages