Function: string-trim

string-trim is a byte-compiled function defined in subr.el.gz.

Signature

(string-trim STRING &optional TRIM-LEFT TRIM-RIGHT)

Documentation

Trim STRING of leading and trailing strings matching TRIM-LEFT and TRIM-RIGHT.

TRIM-LEFT and TRIM-RIGHT default to "[ \\t\\n\\r]+".

Other relevant functions are documented in the string group.

View in manual

Probably introduced at or before Emacs version 24.4.

Shortdoc

;; string
(string-trim " foo ")
    => "foo"

Source Code

;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun string-trim (string &optional trim-left trim-right)
  "Trim STRING of leading and trailing strings matching TRIM-LEFT and TRIM-RIGHT.

TRIM-LEFT and TRIM-RIGHT default to \"[ \\t\\n\\r]+\"."
  (declare (important-return-value t))
  (let* ((beg (and (string-match (if trim-left
                                     (concat "\\`\\(?:" trim-left "\\)")
                                   "\\`[ \t\n\r]+")
                                 string)
                   (match-end 0)))
         (end (string-match-p (if trim-right
                                  (concat "\\(?:" trim-right "\\)\\'")
                                "[ \t\n\r]+\\'")
                              string beg)))
    (if (or beg end)
        (substring string beg end)
      string)))