re: How to split a string (or arbitrary sequence) at each occurrence of a value.

Liste des GroupesRevenir à cl lisp 
Sujet : re: How to split a string (or arbitrary sequence) at each occurrence of a value.
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 05. Jul 2025, 04:40:19
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <104a6r2$19ipg$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Erik Haugan wrote:

* Daniel Pittman <dan...@rimspace.net>
 
What is the best, easiest, fastest, etc, way to split a string into
substrings based on a character position.


(defun string-split (string &optional (delimiter #\Space))
  (with-input-from-string (*standard-input* string)
    (let ((*standard-output* (make-string-output-stream)))
      (loop for char = (read-char nil nil nil)
            if (or (null char)
                   (char= char delimiter))
              collect (get-output-stream-string *standard-output*)
            else
              do (write-char char)
            while char))))

Gauche Scheme

;; Preserves empty strings.
(define (split-on-char text :optional (delimiter #\space))
  (let ((result '()) (tmp '()))
    (string-for-each
      (lambda (c)
        (if (char=? c delimiter)
          (begin
            (push! result tmp)
            (set! tmp '()))
          (push! tmp c)))
      text)
    (when (or (pair? result) (pair? tmp)) (push! result tmp))
    (reverse
      (map
        (lambda(xs) (list->string (reverse xs)))
        result))))


(split-on-char "foo;bar" #\;)
  ===>
("foo" "bar")

(split-on-char ";foo;;bar;" #\;)
  ===>
("" "foo" "" "bar" "")

Date Sujet#  Auteur
5 Jul04:40 o Re: How to split a string (or arbitrary sequence) at each occurrence of a value.1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal