Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Re: trimming a string

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50892] Re: [mg50873] Re: [mg50861] trimming a string
  • From: DrBob <drbob at bigfoot.com>
  • Date: Sun, 26 Sep 2004 05:31:58 -0400 (EDT)
  • References: <ADVMAIL6UC9y3jVON3s00006792@advisormail.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

Here's an even simpler pattern-based solution (but with a flaw fixed at the end of the message). The various solutions beautifully illustrate some of the peculiarities of pattern matching.

s="   abcdefgh   ";
StringJoin[Characters@s/.{" "...,x_,y___," "...}/;x!=" "->{x,y}]//FullForm

"abcdefgh"

If I don't test to make sure x is not blank, the rule removes only the trailing blanks.

s="   abcdefgh   ";
StringJoin[Characters@s/.{" "...,x_,y___," "..}->{x,y}]//FullForm

"   abcdefgh"

Or, again without the test, if the first RepeatedNull is changed to Repeated, then it removes ONE leading blank and all the trailing blanks... but only if there is at least one leading blank; if not, the trailing blanks are untouched.

s="   abcdefgh   ";
StringJoin[Characters@s/.{" "..,x_,y___," "..}->{x,y}]//FullForm

"  abcdefgh"

s="abcdefgh   ";
StringJoin[Characters@s/.{" "..,x_,y___," "..}->{x,y}]//FullForm

"abcdefgh   "

In the first rule above (the one that works), the first pattern (" "...) in the left hand side of the rule is matched to the shortest possible sublist of the subject string so that the whole LHS matches the string. There's no match unless x is non-blank, so that shortest sublist is all the leading blanks. (But without the test, no blanks at all will match. If there's no test and the pattern is " ".. then one blank will match, but only if there are leading blanks.)

x_ is matched to the first non-blank, and y___ is matched to the shortest sublist after x that allows an overall match. The last pattern (" "...) can't include any of the non-blanks after x, so y___ must be matched to everything after x (blank or not) up to the last non-blank. (If the pattern was y__, the rule would fail if x was the only non-blank.)

The same left-to-right progression explains the behavior of all the variations.

Because of the test on x, the rule doesn't match a string of blanks:

s="      ";
StringJoin[Characters@
     s /. {" " ..., x_, y___, " " ...} /; x != " " -> {x, y}] // FullForm

"      "

To handle this requires another rule, so here's a final solution:

stripper = StringJoin[
     Characters@# /. {{" " ..., x_ /; x != " ", y___, "
           " ...} -> {x, y}, {" " ...} -> ""}] &;
stripper /@ {"", "   abcd  efgh   ", "        ", "   a   ",
  "abcd  efgh  ", "   abcd  efgh", "abcd  efgh"} // FullForm

List["", "abcd  efgh", "", "a", "abcd  efgh", "abcd  efgh", "abcd  efgh"]

Bobby

On Sat, 25 Sep 2004 21:11:10 -0500, Akos Beleznay <abeleznay at equitas-capital.com> wrote:

> Bobby,
>
> thanks for your help.  this is that I have looked for.
>
> -akos
>
> -----Original Message-----
> From: DrBob [mailto:drbob at bigfoot.com]
To: mathgroup at smc.vnet.net
> Sent: Saturday, September 25, 2004 3:32 AM
> To: David Park; mathgroup at smc.vnet.net; Akos Beleznay
> Subject: [mg50892] Re: [mg50873] Re: [mg50861] trimming a string
>
> s="   abcdefgh   ";
> StringJoin[Characters@s//.{{" "..,x__}->{x},{x__," "..}->{x}}]//FullForm
>
> "abcdefgh"
>
> or
>
> StringJoin@Flatten[Split@Characters@s/.{{" "..},x__}->{x}/.{x__,{"
> "..}}->{x}]//FullForm
>
> "abcdefgh"
>
> Bobby
>
> On Sat, 25 Sep 2004 01:55:13 -0400 (EDT), David Park
> <djmp at earthlink.net> wrote:
>
>> Akos,
>>
>> One method...
>>
>> trimString[s_String] :=
>>   Module[{ss = s},
>>     While[First@Characters[ss] === " ", ss = StringDrop[ss, 1]];
>>     While[Last@Characters[ss] === " ", ss = StringDrop[ss, -1]];
>>     ss]
>>
>> s = "   abcdefgh   ";
>> trimString[s] // FullForm
>> "abcdefgh"
>>
>> David Park
>> djmp at earthlink.net
>> http://home.earthlink.net/~djmp/
>>
>>
>> From: Akos Beleznay [mailto:abeleznay at equitas-capital.com]
To: mathgroup at smc.vnet.net
>>
>> 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.
>>
>>
>>
>>
>>
>>
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: help on Rewrite rules
  • Next by Date: Re: Arg[z] that works with zero argument?
  • Previous by thread: Re: trimming a string
  • Next by thread: Re: trimming a string