match-split

Name

match-split -- Splits string at target and returns the resulting list of tokens

Synopsis

(match-split string target)

Description

Splits string at every occurance of target and returns the result as a list. Note that match-split returns the occurances of target in the list of tokens.

string

The string to split.

target

The string which is a delimiter between tokens

Example

"this is a test" split at "is" returns ("th" "is" " " "is" " a test")

Author

Norman Walsh, <ndw@nwalsh.com>

Source Code

(define (match-split string target)
  ;; Splits string at target and returns the resulting list of tokens
  (if (string? string)
      (let loop ((result '()) (current "") (rest string))
	(if (< (string-length rest) (string-length target))
	    (append result (if (equal? (string-append current rest) "")
			       '()
			       (list (string-append current rest))))
	    (if (equal? target (substring rest 0 (string-length target)))
		(loop (append result 
			      (if (equal? current "")
				  '()
				  (list current))
			      (list target))
		      ""
		      (substring rest (string-length target) 
				 (string-length rest)))
		(loop result
		      (string-append current (substring rest 0 1))
		      (substring rest 1 (string-length rest))))))
      (list string)))