in reply to change case

What you want is called title case.

Unfortunately, the function associated with it, ucfirst, only does its magic on the first letter of the string, not the first letter of each word.

Try

join ' ' , map ucfirst, split ' '
to do what you want.

Like with upper case, the character escape for title case is \u. It has the same problem as ucfirst above.

Edit:

Never EVER use tr/a-z/A-Z/ for any uppercasing stuff. It doesn't play well with unicode. Not that you were going to, but it's always good to check.

And it looks like davido beat me. Oh well. I must note, though, that I think my solution is faster if the string is especially long, espectially if the words are deliminated by a single space. Then no regex work needs to be done. ;)


Who is Kayser Söze?

Replies are listed 'Best First'.
Re: Re: change case
by davido (Cardinal) on Nov 26, 2003 at 03:18 UTC
    I like your approach too though.

    So we have a s/// (substitution) method, and a join / split / map method. As you mentioned, tr/// isn't a good candidate, but I couldn't resist at least tossing out there the thought of tr/// applied to the first letter of words by using pos along with m/\b[a-z]/g (to locate position of first letters of words) along with substr (as an lvalue to constrain the effects of tr///). ...it could work, but sounds like madness. ;)

    TI(always)MTOWTDI


    Dave


    "If I had my life to live over again, I'd be a plumber." -- Albert Einstein