Menu

#16 String section addition

open
nobody
None
5
2004-05-16
2004-05-16
Chris Capel
No

I'm just getting into lisp, and I was very surprised by
the lack of a function in the ANSI standard for
replacing subsections of vectors (strings) with other
vectors. In C#, you can do something like

mySqlQuery.Replace("'", "\'")

to make it safe for inserting into a DB, and quoting
like that is something you do in tons of places in web
programming. So I think you should include a function
for replacing things in vectors like this. I found this
function on http://www.lisp-p.org/lht/, by Gene Michael
Stover (it was called "string-replace-all-faster" there).

(defun string-replace-all (source old new)
"Replace all occurrences in 'source' of 'old' with
'new'."
(do ((newlen (length new))
(i (search old source)
(search old source :start2 (+ i newlen))))
((null i) source)
(setq source
(concatenate 'string
(subseq source 0 i)
new
(subseq source (+ i newlen))))))

I haven't gotten his permission for anything, I just
found it. I modified the doc string a bit, and I
changed the order and name of the arguments, and the
name of the function.

Maybe there's a library of basic string functions
somewhere you could link to on the string page.

Discussion


Log in to post a comment.