65.9K
CodeProject is changing. Read more.
Home

Convert text to TitleCase

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.53/5 (8 votes)

Feb 19, 2010

CPOL
viewsIcon

22952

In C#, the System.String class contains methods ToUpper and ToLower which convert the case of the string to UPPERCASE and lowercase respectively.But the String class does not have an appropriate method to convert text to TitleCase.No need to worry, the TextInfo class provides this method! ...

In C#, the System.String class contains methods ToUpper and ToLower which convert the case of the string to UPPERCASE and lowercase respectively. But the String class does not have an appropriate method to convert text to TitleCase. No need to worry, the TextInfo class provides this method! :) Here is how you achieve TitleCase:
TextInfo myTextInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;

string sampleStr = "this IS a sample text";
string titleStr = myTextInfo.ToTitleCase(sampleStr);
Console.WriteLine(titleStr);
The above code snippet will print the following: This Is A Sample Text More information regarding TextInfo can be found here.[^]