MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: trimming a string

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50891] Re: [mg50861] trimming a string
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Sun, 26 Sep 2004 05:31:56 -0400 (EDT)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • References: <200409240841.EAA21787@smc.vnet.net>
  • Reply-to: murray at math.umass.edu
  • Sender: owner-wri-mathgroup at wolfram.com

I don't know whether this should be considered "simple", but here's an 
interesting array-oriented way:

(* begin code *)

    nonBlankQ[txt_] := (# ? " ") & /@ txt
    foldIt[op_, lis_] := FoldList[op, First @ lis, Rest @ lis]
    logicalSelect[v_List, b_List] :=
        Cases[Thread[{v, b}], {val_, True} :> val]

    DeleteLeadingTrailingBlanks[s_String] := Module[
       {chars = Characters[s],
          notBlank, notLeading, notTrailing, neither},
       notBlank = nonBlankQ[chars];
       notLeading = foldIt[Or, notBlank];
       notTrailing = Reverse @ foldIt[Or, Reverse @ notBlank];
       neither = Apply[And, #] & /@ Transpose[{notLeading, notTrailing}];
       StringJoin @ logicalSelect[chars, neither]
       ]

    (* example *)
    s = "   he l  lo  ";
    DeleteLeadingTrailingBlanks[s]
    StringLength[%]

(* end code *)

This is a translation of a rather old "idiom" from Ken Iverson's APL 
array-oriented language.  Expressed in Iverson's sequel J (see 
www.jsoftwarse.com) to APL, it takes the particulary terse form:

   dltb=: #~ [: (+./\ *. +./\.) ' '&~:

   NB. For example (input is indented, output flush left):

   dltb '   he l  lo  '
he l  lo
   # dltb '   he l  lo  '
8


Akos Beleznay wrote:
> I would appreciate if somebody could show me a simple way of trimming a
> string. (I did it a hard way, but suspect an elegant solution.)
> 
> Trim(a_string): returns a copy of a string without leading and trailing
> spaces.
> 
> thanks for your help,
> 
> Akos Beleznay
> Equitas Capital Advisors LLC
> 909 Poydras Street
> Suite 1850
> New Orleans, LA 70112
> Phone: (504) 569-9607
> Fax: (504) 569-9650
> abeleznay at equitas-capital.com 
> 
> 
> This email is intended for the sole use of the intended recipient(s) and may contain confidential or privileged information. No one is authorized to copy or re-use this email or any information contained in it. If you are not the intended recipient, we request that you please notify us by reply email and destroy all copies of the message and any attachments. PAFA reserves the right to monitor and review the content of all e-mail communications sent and/or received by its affiliates. Thank you for your cooperation.
> 
> 

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Re: Re: trimming a string
  • Next by Date: Re: 3D graph with assumptions
  • Previous by thread: trimming a string
  • Next by thread: Re: trimming a string