Solved

powershell - title case but also deal with apostrophes

Posted on 2016-10-20
2
34 Views
Last Modified: 2016-10-20
I am changing to title case names.
the command below works Ok

$name=blah
(Get-Culture).textinfo.totitlecase($name)


this gives
mary to Mary
mary jane to Mary Jane
mary-jane to Mary-Jane

but cant seem to deal with apostophes
o'reilly to O'reilly

I need a capital R as well as O

any ideas?
Thanks
0
Comment
Question by:Pete
2 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 41851816
The brute force approach maybe (you can use most non-alphanumeric characters instead of the underscore):
$Name = "o'reilly"
(Get-Culture).textinfo.totitlecase($Name.Replace("'", "_")).Replace("_", "'")

Open in new window

0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 41851829
Maybe someone will come forward with some clever code they have written, but in general formatting names can be a bit of a challenge.  As you have seen apostrophes are not handled by ToTitleCase.  To solve that you would likely want to follow it's use by a replace with a regular expression that looks for an apostrophe followed by a letter and capitalizes the letter.

Keep in mind though there are a lot of other situations that pop up in names, and you probably can't get them all right.  For example "Vincent van Gogh" where the second word should not be capitalized.  Or "McBride" where prefies like Mc and Mac often need to be followed by a capital letter.

Also keep in mind that ToTitleCase does nothing on input strings that are all caps, so if you are using on names it's best to convert to all lower case before passing to ToTitleCase.

~bp
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question