all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* left-trim and right-trim for strings
@ 2002-09-23  8:58 Klaus Berndl
  2002-09-23  9:30 ` Jesper Harder
  2002-09-23 11:28 ` Oliver Scholz
  0 siblings, 2 replies; 9+ messages in thread
From: Klaus Berndl @ 2002-09-23  8:58 UTC (permalink / raw)



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?

Many thanks in advance?
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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: left-trim and right-trim for strings
  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
  1 sibling, 0 replies; 9+ messages in thread
From: Jesper Harder @ 2002-09-23  9:30 UTC (permalink / raw)


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?

I don't think there are.  

I usually end up reusing something from Gnus, when I need it,
e.g. `gnus-simplify-whitespace', `mm-file-name-trim-whitespace' etc.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: left-trim and right-trim for strings
  2002-09-23 11:28 ` Oliver Scholz
@ 2002-09-23 10:02   ` Klaus Berndl
  2002-09-23 10:19   ` Klaus Berndl
  1 sibling, 0 replies; 9+ messages in thread
From: Klaus Berndl @ 2002-09-23 10:02 UTC (permalink / raw)


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 ]*$"))

Dirty but also charming :-)

I think i will use it.

Thanks,
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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: left-trim and right-trim for strings
  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>
  1 sibling, 1 reply; 9+ messages in thread
From: Klaus Berndl @ 2002-09-23 10:19 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: left-trim and right-trim for strings
  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
  1 sibling, 2 replies; 9+ messages in thread
From: Oliver Scholz @ 2002-09-23 11:28 UTC (permalink / raw)


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

-- 
2 Vendémiaire an 211 de la Révolution
Liberté, Egalité, Fraternité!

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: left-trim and right-trim for strings
  2002-09-23 10:19   ` Klaus Berndl
@ 2002-09-23 18:45     ` Stefan Monnier <foo@acm.com>
  2002-09-24  5:11       ` maierh
  0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2002-09-23 18:45 UTC (permalink / raw)



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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: left-trim and right-trim for strings
  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
  0 siblings, 2 replies; 9+ messages in thread
From: maierh @ 2002-09-24  5:11 UTC (permalink / raw)



"Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> writes:

> 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:

> 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))) ?
>

Since emacs-21 we have too `replace-regexp-in-string'. Personally, I
like it very much.

,----
| (replace-regexp-in-string "^ *" "" "  This is a string.  ")
`----

Harald

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: left-trim and right-trim for strings
  2002-09-24  5:11       ` maierh
@ 2002-09-24  5:24         ` Benjamin Lewis
  2002-09-24 13:09         ` Klaus Berndl
  1 sibling, 0 replies; 9+ messages in thread
From: Benjamin Lewis @ 2002-09-24  5:24 UTC (permalink / raw)


On Tue, 24 Sep 2002, maierh@myself.com wrote:

> 
> "Stefan Monnier <foo@acm.com>"
> <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> writes:
> 
>> 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:
> 
>> 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))) ?
>> 
> 
> Since emacs-21 we have too `replace-regexp-in-string'. Personally, I
> like it very much.
> 
> ,----
>> (replace-regexp-in-string "^ *" "" "  This is a string.  ")
> `----

Hey, cool.  You could do (replace-regexp-in-string "[ \t\n]+" " " "    This
is  another	string.   ")

to remove doubled spaces.

-- 
Benjamin Lewis

Don't take life so serious, son, it ain't nohow permanent.
                -- Walt Kelly

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: left-trim and right-trim for strings
  2002-09-24  5:11       ` maierh
  2002-09-24  5:24         ` Benjamin Lewis
@ 2002-09-24 13:09         ` Klaus Berndl
  1 sibling, 0 replies; 9+ messages in thread
From: Klaus Berndl @ 2002-09-24 13:09 UTC (permalink / raw)


On Tue, 24 Sep 2002, maierh@myself.com wrote:

>  
>  "Stefan Monnier <foo@acm.com>"
>  <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> writes:
>  
> > 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:
>  
> > 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))) ?
> >
>  
>  Since emacs-21 we have too `replace-regexp-in-string'. Personally, I
>  like it very much.
>  
>  ,----
>  | (replace-regexp-in-string "^ *" "" "  This is a string.  ")
>  `----

Eureka! Yes, indeed, this is the one and only solution. It can be used for
left- right- and middle-trim without any if etc...Great.

But why are such trimming functions not included in the elisp-library? IMHO
they would be very useful for elisp-programmers?!

Klaus

P.S.
Nevertheless Stefans adjudgement about my first using of string-match and
split-string is correct ;-)


-- 
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

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-09-24 13:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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>
2002-09-24  5:11       ` maierh
2002-09-24  5:24         ` Benjamin Lewis
2002-09-24 13:09         ` Klaus Berndl

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.