Click here to Skip to main content
15,892,537 members
Articles / Programming Languages / C#
Tip/Trick

Convert text to TitleCase

Rate me:
Please Sign up or sign in to vote.
3.53/5 (8 votes)
21 Feb 2010CPOL 22.2K   1   3
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.[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
An individual with more than a decade of experience in desktop computing and mobile app development primarily on the Microsoft platform. He loves programming in C#, WPF & XAML related technologies.
Current interests include web application development, developing rich user experiences across various platforms and exploring his creative side.

Ratish's personal blog: wpfspark.wordpress.com

Comments and Discussions

 
QuestionComment Please Pin
dotnetpickles9-Feb-14 19:45
dotnetpickles9-Feb-14 19:45 
GeneralReason for my vote of 3 This is a good method for C#, or eve... Pin
yannduran23-Aug-11 18:19
yannduran23-Aug-11 18:19 
Reason for my vote of 3
This is a good method for C#, or even VB, but unfortunately can't be used in Silverlight (& therefore also LightSwitch), otherwise I would have given you a 5.
GeneralWhat About Silverlight? Pin
Alexeins16-Oct-10 17:55
Alexeins16-Oct-10 17:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.