From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Klaus Berndl Newsgroups: gmane.emacs.help Subject: Re: left-trim and right-trim for strings Date: 23 Sep 2002 12:19:52 +0200 Organization: sd&m AG, Muenchen, Germany Sender: help-gnu-emacs-admin@gnu.org Message-ID: References: NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: main.gmane.org 1032778564 24955 127.0.0.1 (23 Sep 2002 10:56:04 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Mon, 23 Sep 2002 10:56:04 +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 17tQsi-0006Tr-00 for ; Mon, 23 Sep 2002 12:56:00 +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 17tQZi-0001hl-00; Mon, 23 Sep 2002 06:36:22 -0400 Original-Path: shelby.stanford.edu!nntp.stanford.edu!newsfeed.stanford.edu!headwall.stanford.edu!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!news.teledanmark.no!newsfeed1.ulv.nextra.no!nextra.com!bnewspeer00.bru.ops.eu.uu.net!emea.uu.net!news.sdm.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 63 Original-NNTP-Posting-Host: hickerstall.muc.sdm.de Original-X-Trace: solti3.muc.sdm.de 1032776393 23554 193.102.183.204 (23 Sep 2002 10:19:53 GMT) Original-X-Complaints-To: usenet@news.sdm.de Original-NNTP-Posting-Date: 23 Sep 2002 10:19:53 GMT User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1 Original-Xref: nntp.stanford.edu gnu.emacs.help:105167 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:1722 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:1722 On Mon, 23 Sep 2002, Oliver Scholz wrote: > Klaus Berndl writes: > > > left-trim: Removing any leading whitespace from a string. > > right-trim: Removing any trailing whitespace from a string. > > > > Are there such functions available in elisp or have i to write them > > for myself? > > Probably rather a dirty trick, but you could abuse `split-string': > > (car (split-string " Dies ist ein Test." "^[\n\t ]*")) > (car (split-string "Dies ist ein Test. " "[\n\t ]*$")) > > -- Oliver Here is a set of string trimming functions: ,---- | (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)) | | (defun str-left-trim (str) | "Return a string stripped of all leading whitespaces of STR." | (or (car (split-string str "^[\n\t ]*")) "")) | | (defun str-right-trim (str) | "Return a string stripped of all trailing whitespaces of STR." | (or (car (split-string str "[\n\t ]*$")) "")) | | (defun str-trim (str) | "Applies `str-right-trim' and `str-left-trim' to STR." | (str-left-trim (str-right-trim str))) | | (defun str-full-trim (str) | "Applies `str-trim' and `str-middle-trim' to STR." | (str-excessive-trim (str-trim str))) `---- Thanks to Oliver and Jesper! Any further suggestions? Ciao, Klaus -- Klaus Berndl mailto: klaus.berndl@sdm.de sd&m AG http://www.sdm.de software design & management Thomas-Dehler-Str. 27, 81737 München, Germany Tel +49 89 63812-392, Fax -220