A correct "titlization" will not capitalize "small words" (articles,
prepositions etc). This is probably overkill but I've got parser madness and
just could not help myself =)

The attached mcz package will require Lukas Renggli's PetitParser from the
repository at http://source.lukas-renggli.ch/petit

Once loaded you can evaluate 'of mice and men' asTitle to get 'Of Mice and
Men'

John


On Sat, Mar 20, 2010 at 11:18 AM, David T. Lewis <le...@mail.msen.com>wrote:

> I should note also that class Character has methods for testing
> different kinds of characters (#isDigit and so forth). So if you
> wanted your method to work for strings that might contain tab
> characters or carriage returns as separator characters between
> words, then you would use the #isSeparator test rather than looking
> for space characters explicitly, like this:
>
> String>>titleize
>    "Answer a copy of myself with the first character of each word
> capitalized "
>    " 'a mouse ate cheese' titleize "
>    ^ String streamContents: [:strm | self
>            inject: Character space
>            into: [:last :this | strm
>                     nextPut: (last isSeparator
>                                 ifTrue: [this asUppercase]
>                                ifFalse: [this])]]
>
>
> On Sat, Mar 20, 2010 at 11:05:02AM -0400, David T. Lewis wrote:
> > On Fri, Mar 19, 2010 at 02:31:40AM -0400, sergio_101 wrote:
> > > is there a function that can do this:
> > >
> > > 'a mouse ate cheese' . 'A Mouse Ate Cheese'
> > >
> > > i found capitlize, but that only hits the first word..
> >
> > Here is another way to do it:
> >
> > String>>titleize
> >     "Answer a copy of myself with the first character of each word
> capitalized "
> >     " 'a mouse ate cheese' titleize "
> >     ^ String streamContents: [:strm | self
> >             inject: Character space
> >             into: [:last :this | strm
> >                     nextPut: (last = Character space
> >                                 ifTrue: [this asUppercase]
> >                                 ifFalse: [this])]]
> >
> > Explanation:
> >
> > The #inject:into: steps through the string, keeping track of the
> > preceding character and writing the current character to a stream,
> > converting it to upper case if the preceding character was a space.
> >
> > The result of the #nextPut: is the character we wrote, which appears
> > in the #last variable each time we step through the loop.
> >
> > The #streamContents: method provides the stream that we write the
> > characters to, and answers the resulting string when complete.
> >
>
> _______________________________________________
> Beginners mailing list
> Beginners@lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>



-- 
http://jmck.seasidehosting.st

Attachment: Parsers-jmck.1.mcz
Description: Binary data

_______________________________________________
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners

Reply via email to