all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Klaus Berndl <Klaus.Berndl@sdm.de>
Subject: Re: left-trim and right-trim for strings
Date: 23 Sep 2002 12:19:52 +0200	[thread overview]
Message-ID: <uadm9m2if.fsf@sdm.de> (raw)
In-Reply-To: m3znu9gd2w.fsf@ID-87814.user.dfncis.de

On Mon, 23 Sep 2002, Oliver Scholz wrote:



>  Klaus Berndl <Klaus.Berndl@sdm.de> 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

  parent reply	other threads:[~2002-09-23 10:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-23  8:58 left-trim and right-trim for strings Klaus Berndl
2002-09-23  9:30 ` Jesper Harder
2002-09-23 11:28 ` Oliver Scholz
2002-09-23 10:02   ` Klaus Berndl
2002-09-23 10:19   ` Klaus Berndl [this message]
2002-09-23 18:45     ` Stefan Monnier <foo@acm.com>
2002-09-24  5:11       ` maierh
2002-09-24  5:24         ` Benjamin Lewis
2002-09-24 13:09         ` Klaus Berndl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=uadm9m2if.fsf@sdm.de \
    --to=klaus.berndl@sdm.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.