1

I would like to concatenate a string (e.g. "abc") and a list (e.g. '(aa bb)) so as to obtain "abc aa bb".

Drew
  • 77,472
  • 10
  • 114
  • 243
Francesco Cadei
  • 347
  • 3
  • 17

3 Answers3

5

AFAIK the canonical way to turn a list into a string is with mapconcat, e.g.

(defun concat-string-list (str xs)
  (concat str " "
          (mapconcat #'symbol-name xs " ")))
Stefan
  • 26,404
  • 3
  • 48
  • 85
3

Does the content of the list always consist of symbols like that? If you just want the string representation of the content of the list (i.e. the printed list without the parens) you could do something like:

(let* ((my/str "abc ")
       (my/list '(aa bb)))
  (concat
   my/str
   (substring (format "%s" my/list) 1 -1)))

;; "abc aa bb"
ebpa
  • 7,449
  • 29
  • 55
1

This is another method that assumes the list always contains symbols. It uses #'symbol-name to convert each symbol to a string.

ELISP> (defun string-and-list-separated-by-space (string lst)
  (string-join (cons string
                     (mapcar #'symbol-name
                             lst))
               " "))
string-and-list-separated-by-space
ELISP> (string-and-list-separated-by-space "abc" '(aa bb))
"abc aa bb"
zck
  • 9,092
  • 2
  • 33
  • 65