Function: string-pad
string-pad is an autoloaded and byte-compiled function defined in
subr-x.el.gz.
Signature
(string-pad STRING LENGTH &optional PADDING START)
Documentation
Pad STRING to LENGTH using PADDING.
If PADDING is nil, the space character is used. If not nil, it should be a character.
If STRING is longer than the absolute value of LENGTH, no padding is done.
If START is nil (or not present), the padding is done to the end of the string, and if non-nil, padding is done to the start of the string.
Other relevant functions are documented in the string group.
Probably introduced at or before Emacs version 28.1.
Shortdoc
;; string
(string-pad "foo" 5)
=> "foo "
(string-pad "foobar" 5)
=> "foobar"
(string-pad "foo" 5 45 t)
=> "--foo"
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/subr-x.el.gz
;;;###autoload
(defun string-pad (string length &optional padding start)
"Pad STRING to LENGTH using PADDING.
If PADDING is nil, the space character is used. If not nil, it
should be a character.
If STRING is longer than the absolute value of LENGTH, no padding
is done.
If START is nil (or not present), the padding is done to the end
of the string, and if non-nil, padding is done to the start of
the string."
(declare (pure t) (side-effect-free t))
(unless (natnump length)
(signal 'wrong-type-argument (list 'natnump length)))
(let ((pad-length (- length (length string))))
(cond ((<= pad-length 0) string)
(start (concat (make-string pad-length (or padding ?\s)) string))
(t (concat string (make-string pad-length (or padding ?\s)))))))