Skip to content

Instantly share code, notes, and snippets.

@kobapan
Last active October 11, 2017 08:30
Show Gist options
  • Save kobapan/da10f10a11def6c9886a to your computer and use it in GitHub Desktop.
Save kobapan/da10f10a11def6c9886a to your computer and use it in GitHub Desktop.
文字列の先頭及び末尾から空白(又は指定文字)を取り除く emacs lisp
(defun trim (string &optional needle)
"Remove needle at the beginning and endding of string."
(let ((n (if needle needle "[ \t\n\r]+"))
(s (if (string-match (concat "\\`" n) string) ; "\`" バッファや文字列の先頭
(replace-match "" t t string)
string)))
(if (string-match (concat n "+\\'") s) ; "\'" バッファや文字列の末尾
(replace-match "" t t s)
s)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment