[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

powershell - title case but also deal with apostrophes

Posted on 2016-10-20
2
Medium Priority
?
79 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 85

Accepted Solution

by:
oBdA earned 1000 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 57

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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