Solved

powershell - title case but also deal with apostrophes

Posted on 2016-10-20
2
17 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 82

Accepted Solution

by:
oBdA earned 250 total points
Comment Utility
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 51

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now