Is there a function in Emacs Lisp that does the opposite of split-string, i.e. joins the elements of a list into string delimited by a given delimiter? In other words, is there a function that given a list, e.g. ("foo" "bar" "baz"), and a delimiter, e.g. ", ", returns that list as a string delimited by that delimiter, e.g. "foo, bar, baz".

Common Lisp seems to have such a function but the function with the same name in Emacs Lisp, format, is a wholly different function.

share|improve this question
up vote 24 down vote accepted

I think you are looking for mapconcat:

mapconcat applies function to each element of sequence: the results, which must be strings, are concatenated. Between each pair of result strings, mapconcat inserts the string separator. Usually separator contains a space or comma or other suitable punctuation.

share|improve this answer
20  
2  
The example made this answer much more useful. – Ben Key Nov 5 '15 at 19:45
1  
default package of emacs 24.5 24.5/lisp/emacs-lisp/subr-x.el has the same function as mentioned by @N.N. named string-join. – user927387 Aug 27 '16 at 12:08

If you use string manipulation heavily in your Elisp, i recommend you to install a library s.el, which has a vast and consistent API for strings. For your task it has a function s-join:

(s-join "+" '("abc" "def" "ghi")) ;; => "abc+def+ghi"
(s-join "\n" '("abc" "def" "ghi")) ;; => "abc\ndef\nghi"
share|improve this answer

Emacs 25 has string-join:

(string-join '("one" "two" "three") ", ") ; ==> "one, two, three"
share|improve this answer

Depending what you're doing, there may be a better function eg if you're building command line args, use combine-and-quote-strings which does the work for you.

(combine-and-quote-strings '("foo" "bar" "baz") ", ")
"foo, bar, baz"
share|improve this answer
    
to me, this seems like the "correct" answer. – robert Oct 18 at 9:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.