Solved

powershell - title case but also deal with apostrophes

Posted on 2016-10-20
2
39 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A procedure for exporting installed hotfix details of remote computers using powershell
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scannerā€¦
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

739 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