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?
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.
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.
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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With