String to List

See also List to String

Function Syntax (LM:str->lst <str> <del>)
Current Version 1.0
Donate
Arguments
Symbol Type Description
str String A delimited string to parse
del String Delimiter by which to separate each list element
Returns
Type Description
List A list of strings parsed from 'str' separated by delimiter 'del'

Function Description

This function parses a string using a supplied delimiter, and returns a list of strings.

In other words, the function will separate or break the given string at each occurrence of the delimiter string; this functionality is more clearly demonstrated by the example function call shown below.

The converse of this function is List to String.

Recursive Version

Select all
;; String to List  -  Lee Mac
;; Separates a string using a given delimiter
;; str - [str] String to process
;; del - [str] Delimiter by which to separate the string
;; Returns: [lst] List of strings
 
(defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)
    )
)

Iterative Version

Select all
;; String to List  -  Lee Mac
;; Separates a string using a given delimiter
;; str - [str] String to process
;; del - [str] Delimiter by which to separate the string
;; Returns: [lst] List of strings

(defun LM:str->lst ( str del / len lst pos )
    (setq len (1+ (strlen del)))
    (while (setq pos (vl-string-search del str))
        (setq lst (cons (substr str 1 pos) lst)
              str (substr str (+ pos len))
        )
    )
    (reverse (cons str lst))
)

Example Function Call

_$ (LM:str->lst "1,2,3,4,5" ",")

("1" "2" "3" "4" "5")

See also List to String

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010