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

前端 未结 6 1927
慢半拍i
慢半拍i 2020-12-03 01:12

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

(apply #\'concatenate \'string \'(\"         


        
6条回答
  •  借酒劲吻你
    2020-12-03 01:57

    Assuming a list of strings and a single character delimiter, the following should work efficiently for frequent invocation on short lists:

    (defun join (list &optional (delimiter #\&))
      (with-output-to-string (stream)
        (join-to-stream stream list delimiter)))
    
    (defun join-to-stream (stream list &optional (delimiter #\&))
      (destructuring-bind (&optional first &rest rest) list
        (when first
          (write-string first stream)
          (when rest
            (write-char delimiter stream)
            (join-to-stream stream rest delimiter)))))
    

提交回复
热议问题