Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize names

Tags:

c#

.net

Basically if I want to transform a name from

stephen smith 

to

Stephen Smith 

I can easily do it with come CSS on the page, but ideally I would like to catch it earlier on and change it when it comes out of the database. How can I get C# to capitalise a string?

Is there a function for this?

like image 301
Steve Avatar asked Feb 20 '11 15:02

Steve


People also ask

Do you capitalize all names?

Names are proper nouns. The names of cities, countries, companies, religions, and political parties are also proper nouns, so you should capitalize them, too. We experienced some beautiful Southern California weather last fall when we attended a Catholic wedding in San Diego.

How do you correctly capitalize?

In English, a capital letter is used for the first word of a sentence and for all proper nouns (words that name a specific person, place, organization, or thing). In some cases, capitalization is also required for the first word in a quotation and the first word after a colon.


2 Answers

You can do this using the ToTitleCase method of the System.Globalization.TextInfo class:

CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; TextInfo textInfo = cultureInfo.TextInfo;  Console.WriteLine(textInfo.ToTitleCase(title)); Console.WriteLine(textInfo.ToLower(title)); Console.WriteLine(textInfo.ToUpper(title)); 
like image 150
Dexter Avatar answered Oct 06 '22 01:10

Dexter


Names are tricky. The simple rules of First Letters do not apply. The only sensible approach here is to ask your users how they want it. Anything else can cause offence.

If my name is MacPhearson, ODowel, or just simply marc, Marc or even mArC - then frankly: leave it alone. Trust the user to get it right. This gets even more tricky as you go between cultures.

like image 31
Marc Gravell Avatar answered Oct 05 '22 23:10

Marc Gravell