From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "Stefan Monnier " Newsgroups: gmane.emacs.help Subject: Re: left-trim and right-trim for strings Date: 23 Sep 2002 14:45:29 -0400 Organization: Yale University Sender: help-gnu-emacs-admin@gnu.org Message-ID: <5l3cs0o88m.fsf@rum.cs.yale.edu> References: NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1032807978 24306 127.0.0.1 (23 Sep 2002 19:06:18 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Mon, 23 Sep 2002 19:06:18 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 17tYXA-0006Ju-00 for ; Mon, 23 Sep 2002 21:06:16 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10) id 17tYXD-0001st-00; Mon, 23 Sep 2002 15:06:19 -0400 Original-Path: shelby.stanford.edu!nntp.stanford.edu!newsfeed.stanford.edu!newsfeed.berkeley.edu!news-hog.berkeley.edu!ucberkeley!news.ycc.yale.edu!rum.cs.yale.edu!rum.cs.yale.edu Original-Newsgroups: gnu.emacs.help Original-Lines: 37 Original-NNTP-Posting-Host: rum.cs.yale.edu User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 X-Original-NNTP-Posting-Host: rum.cs.yale.edu X-Original-Trace: 23 Sep 2002 14:45:30 -0400, rum.cs.yale.edu Original-Xref: nntp.stanford.edu gnu.emacs.help:105198 Original-To: help-gnu-emacs@gnu.org Errors-To: help-gnu-emacs-admin@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.0.11 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.help:1753 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:1753 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