unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.help/news/@flint.cs.yale.edu>
Subject: Re: left-trim and right-trim for strings
Date: 23 Sep 2002 14:45:29 -0400	[thread overview]
Message-ID: <5l3cs0o88m.fsf@rum.cs.yale.edu> (raw)
In-Reply-To: uadm9m2if.fsf@sdm.de


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

  reply	other threads:[~2002-09-23 18:45 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
2002-09-23 18:45     ` Stefan Monnier <foo@acm.com> [this message]
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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=5l3cs0o88m.fsf@rum.cs.yale.edu \
    --to=monnier+gnu.emacs.help/news/@flint.cs.yale.edu \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).