Link to home
Start Free TrialLog in
Avatar of Marketing_Insists
Marketing_Insists

asked on

cases for multi-word strings

# Setting the case of a last name is done like so:
$last = ucfirst lc("sampson");
gives "Sampson"

# What about setting case for a multi-word string? e.g.;
$last = ucfirst lc("sampson-smith, jr");
gives Samson-smith, jr

But how can I get "Sampson-Smith, Jr" , where each portion of the proper name is capitalized?
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To explain the regex...
\b = zero-width word-break assertion
\w+ = as many word characters as possible

flags:
g = repeat as many times as possible
e = execute the code in the rhs of the substitution
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First thing that comes to mind is something like this but it doesn't quite work...

$last = join ' ', map { ucfirst $_ } split /-,\s\s*/, 'sampson-smith, jr';
join '', map { ucfirst } split /(-|\s+)/, 'sampson-smith, jr';
#or
join '', map { ucfirst } split /([-,\s])/, 'sampson-smith, jr';
#or
join '', map { ucfirst } split /(?<=-|\s)/, 'sampson-smith, jr';  
#or
join '', map { ucfirst } split /(\W+)/, 'sampson-smith, jr';
#or
join '', map { ucfirst } split /\b/, 'sampson-smith, jr';  
#might have worked
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marketing_Insists
Marketing_Insists

ASKER

Thanks
CPAN module may come in handy later with MacDonald vs Macer issues
4) For simplicity, I would simply divide the points evenly between the four contributors since all four answered the question accurately:
http:#a38803391
http:#a38803401
http:#a38803612
http:#a38812799