FWIW
;;;------------------splitstring------------------
;;; Purpose: Split a string into shorter strings
;;; Parameters:
;;; in : string to process
;;; # : maximum length of string to return
;;; char : character delimiter
;;; Return: list of strings
;;; Author : Herman Mayfarth
;;; Date: June 2014
;;;-----------------------------------------------
(defun splitstring
(in char #
/ stripleft
stripright
chopright
split
out)
;;local functions
;;strip char from left side
(defun stripleft
(string char
) string
);if
string ; return empty string
);if
);stripleft
;;strip char from right side
(defun stripright
(string char
) string
);if
string ; return empty string
);if
);stripright
;;strip non-chars from right side until next char is found
(defun chopright
(string char
) string
);if
string ; return empty string
);if
);chopright
;;split string into chunks <= n characters
;;recursive, but creates out list as a side effect
(defun split
(string char n
/ chunk m
) (if (= (strlen chunk
) n
);string has at least n characters (= (substr string
(1+ n
) 1) char
)) ;case I - end of word )
(not (= (substr string
(1+ n
) 1) char
)));case II - middle of word (strlen (setq chunk
(stripright
(chopright chunk char
) char
)))) )
((= (substr chunk n
) char
) ;case III - delimiter out
(cons (stripright chunk char
) out
)) )
);cond
(split string char n)
);progn
);if
);split
;;main program
(setq in
(stripright
(stripleft in char
) char
));strip leading & trailing chars (split in char #)
);splitstring
test string:
(setq b
" Tomorrow and tomorrow and tomorrow. Life creeps on in its petty pace from day to day, to the last syllable of recorded time. ")
Test:
Command: (splitstring b sp 25)
("Tomorrow and tomorrow and" "tomorrow. Life creeps on" "in its petty pace from" "day to day, to the last" "syllable of recorded" "time.")
I really don't like it, because out is built as a side effect, not a pure function call.
Also, it fails for this case:
Command: (splitstring b sp 5)
Hard error occurred ***
internal stack limit reached (simulated)
Oh, well.