|
From: | Stefan Monnier <address@hidden> |
Subject: | Re: left-trim and right-trim for strings |
Date: | 23 Sep 2002 14:45:29 -0400 |
User-agent: | Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 |
How ironic. You used `split-string' just where `string-match' makes more sense and then use `string-match' where `split-string' is just what you need: > | (defun str-excessive-trim (str) > | "Return a string where all double-and-more whitespaces in STR are replaced > | with a single space-character." > | (let ((s str)) > | (while (string-match "[ \t][ \t]+" s) > | (setq s (concat (substring s 0 (match-beginning 0)) > | " " > | (substring s (match-end 0))))) > | s)) How about (mapconcat 'identity " " (split-string str "[ \t][ \t]")) ? > | (defun str-left-trim (str) > | "Return a string stripped of all leading whitespaces of STR." > | (or (car (split-string str "^[\n\t ]*")) "")) Why not (if (string-match "\\`[\n\t ]+" str) (substring str (match-end 0)) str) ? or (if (string-match "\\`[\n\t ]*" str) (substring str (match-end 0))) ? > | (defun str-right-trim (str) > | "Return a string stripped of all trailing whitespaces of STR." > | (or (car (split-string str "[\n\t ]*$")) "")) How about (substring str 0 (string-match "[\n\t ]*\\'" str)) -- Stefan
[Prev in Thread] | Current Thread | [Next in Thread] |