Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function that joins a string into a delimited string?

Tags:

elisp

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.

like image 844
N.N. Avatar asked Oct 21 '12 16:10

N.N.


People also ask

Which function joins two strings together?

The CONCATENATE function joins two text strings into one text string.

How do you join strings?

The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

Which string function splits a string based on a delimiter?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

What is it called when you join a string to a variable?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect.


2 Answers

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.

like image 109
Volker Stolz Avatar answered Sep 22 '22 14:09

Volker Stolz


Emacs has string-join since 24.4 as part of subr-x.el:

(eval-when-compile (require 'subr-x)) (string-join '("one" "two" "three") ", ") ; ==> "one, two, three" 

subr-x.el contains also other useful (string manipulation) functions - from the 24.4 release notes:

New library subr-x.el for misc helper functions  hash-table-keys hash-table-values string-blank-p string-empty-p string-join string-reverse string-trim-left string-trim-right string-trim string-remove-prefix string-remove-suffix 
like image 28
Joost Kremers Avatar answered Sep 22 '22 14:09

Joost Kremers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!