MathGroup Archive 2006

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

Search the Archive

Re: StringReplace and WordBoundary

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67670] Re: StringReplace and WordBoundary
  • From: bghiggins at ucdavis.edu
  • Date: Tue, 4 Jul 2006 01:57:19 -0400 (EDT)
  • References: <e8as9s$g6k$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Bruce,

You need to account for the whitespace in your string. Try this

X = "abba  butter bbb a";
StringReplace[X, StartOfString | (WordBoundary ~~ Whitespace) ~~ y_ ->
ToUpperCase[y]]

AbbaButterBbbA


Cheers,

Brian

Bruce Colletti wrote:
> Re v5.2.0.0.
>
> This code capitalizes each word's first letter:
>
> X="abba  butter bbb a";
> StringReplace[X,WordBoundary~~y_->ToUpperCase@y]
>
> "Abba  Butter Bbb A"
>
> However, I mistakenly thought StringReplace would substitute the full "WordBoundary~~y_" pattern by a capital letter to give "AbbaButterBbbA".  How do I change the code to do this?
>
> The StringReplace help text says "replaces every SUBSTRING that matches lhs."  Is it this focus upon the substring that explains why the above code leaves WordBoundary untouched?  Or is the "real" reason something else?
> 
> Thanks.
> 
> Bruce


  • Prev by Date: Re: Position
  • Next by Date: text Offset Background behavior
  • Previous by thread: Re: StringReplace and WordBoundary
  • Next by thread: RE: StringReplace and WordBoundary