There are many routines on this Forum about splitting text strings apart around whatever delimiting character you like. Some Searching will find a lot of options.
Hi,
You can find many routines to split a string into a list.
Here're two examples:
Iterative
(defun str2lst (str sep / len lst)
(setq len (strlen sep))
(while (setq pos (vl-string-search sep str))
(setq lst (cons (substr str 1 pos) lst)
str (substr str (+ len pos 1))
)
)
(reverse (cons (substr str 1 pos) lst))
)
Recursive:
(defun str2lst (str sep / pos) (if (setq pos (vl-string-search sep str)) (cons (substr str 1 pos) (str2lst (substr str (+ (strlen sep) pos 1)) sep)) (list str) ) )
(str2lst '("1 2 3 4 5") " ") returns ("1" "2" "3" "4" "5")
Here's some ancient history...
;; This function takes a delimited string, and returns a list:
;;------------------------------
;;Don: (02-09-02)
;;
;;First let me point out that the one you call JohnU is actually
;;Marc'Antonio's from November.
;;
;;As Marc'Antonio sagely reminded us, setting string symbols is slower than
;;setting integers.
;;
;;Your offering is the almost the same a Luis' except that yours doesn't
;;provide for validating the input or for multiple-character delimiters.
;;
;;While Marc'Antonio's method is the fastest, I've grown a liking to the
;;multiple-character delimiter, which his will not handle.
;;
;;Taking the best from Luis and Marc'Antonio, plus acknowledgments to Eric
;;Schneider, I now offer the following...
(defun @Anonymous_str2list (str pat / i j n lst)
(cond
((/= (type str)(type pat) 'STR))
((= str pat)'(""))
(T
(setq i 0 n (strlen pat))
(while (setq j (vl-string-search pat str i))
(setq lst (cons (substr str (1+ i)(- j i)) lst)
i (+ j n)
)
)
(reverse (cons (substr str (1+ i)) lst))
)
)
)
;; It's not as fast as Marc'Antonio's, but pretty close.
Just one thing... you showed your string actually as a list of a string. I doubt you really meant that, but it's okay because you can process the string within the list.
John F. Uhden
@doglips wrote:
How do I change ("1 2 3 4 5") to ("1" "2" "3" "4" "5") ?
Another
(mapcar 'itoa
(read (strcat "(" (Car lst) ")")))
("1" "2" "3" "4" "5")
or just
(read (strcat "(" (Car lst) ")"))
(1 2 3 4 5)
But more importantly , how did you end up this that list in the first place?
Maybe what you need is a routine that will build the list to be like the end result to begin with.
Is that from a list box multiple selection value?
@doglips wrote:
The user wanted to see the input at the command line so I used Getstring
I see, and is it always numbers? Why do you need individual strings? what are you planning to use the for?
Anyway, try this
(mapcar 'itoa (read (strcat "(" (lisped "Type it here") ")")))
way cooler than command prompt for strings
Lee Mac ( @Lee_Mac) has a great collection of utilities.
Here are few examples for converting a string to a list:
http://www.lee-mac.com/stringtolist.html
Regards,
Jerry
You must doTrim inside Text " Hellow World" , the result must be (list "Hello" "World").
You must replace repetitve Spaces into one-space
This function is very-usefull for (read-line file-text-fromDisk)
Command: R25
16="(This function takes a delimited string, and returns a list)"=" This
function takes a delimited string, and returns a list "
11="(Hello World)"=" Hello World
"C:r25"
John F. Uhden
What about if you want two characters as one string ? Then use comma as part of the getstring eg "1,2,3 4,A,B,C D E" then same method but comma.