Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the canonical way to join strings in a list?

Tags:

common-lisp

I want to convert ("USERID=XYZ" "USERPWD=123") to "USERID=XYZ&USERPWD=123". I tried

(apply #'concatenate 'string '("USERID=XYZ" "USERPWD=123")) 

which will return ""USERID=XYZUSERPWD=123".

But i do not know how to insert '&'? The following function works but seems a bit complicated.

(defun join (list &optional (delim "&"))     (with-output-to-string (s)         (when list             (format s "~A" (first list))             (dolist (element (rest list))                (format s "~A~A" delim element))))) 
like image 615
z_axis Avatar asked Jan 12 '12 06:01

z_axis


People also ask

How do I join a list into a string?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

How do you combine strings in a list in Python?

You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

What is joining strings together?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs.


1 Answers

Use FORMAT.

~{ and ~} denote iteration, ~A denotes aesthetic printing, and ~^ (aka Tilde Circumflex in the docs) denotes printing the , only when something follows it.

* (format nil "~{~A~^, ~}" '( 1 2 3 4 ))  "1, 2, 3, 4" *  
like image 130
Paul Nathan Avatar answered Sep 23 '22 12:09

Paul Nathan