Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a native Proper Case string function in C#? [duplicate]

Tags:

string

c#

I was about to write my own C# extension to convert a string to Proper Case (i.e. capitalize the first letter of every word), then I wondered if there's not a native C# function to do just that... is there?

like image 973
Shaul Behr Avatar asked Jan 05 '10 09:01

Shaul Behr


People also ask

What is ToTitleCase?

The ToTitleCase method is used to capitalize the first letter in a word. Title case itself means to capitalize the first letter of each major word.

How do you convert all strings to title caps in a string array?

Convert all the elements in each and every word in to lowercase using string. toLowerCase() method. Loop through first elements of all the words using for loop and convert them in to uppercase.


2 Answers

String s  = "yOu caN Use thIs"  s = System.Threading.Thread.CurrentThread            .CurrentCulture.TextInfo.ToTitleCase(s.ToLower()); 

The main limitation I can see with this is that it's not "true" title case. i.e. In the phrase "WaR aNd peaCe", the "and" part should be lowercase in English. This method would capitalise it though.

like image 96
Jamie Dixon Avatar answered Oct 06 '22 06:10

Jamie Dixon


There is a function that capitalises the first letters of words, though you should see the remarks section as it does have some limitations which may make it unsuitable for your needs.

like image 26
Greg Beech Avatar answered Oct 06 '22 08:10

Greg Beech