Proper casing of the strings

Many a times we are required to show strings with proper casings. The TextInfo  property from System.Globalization provides culture-specific casing information for strings. This has the advantage that it is culture aware and thus can be used for any country specific culture. This is how you would use it.

Two specific namespaces required for this:

using System.Globalization;
using System.Threading;

protected void Page_Load(object sender, EventArgs e)
{
   string myTitle = "to Proper casing";
   CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
   TextInfo textInfo = cultureInfo.TextInfo;

   Console.WriteLine(textInfo.ToTitleCase(myTitle)); 
   Console.WriteLine(textInfo.ToLower(myTitle));     
   Console.WriteLine(textInfo.ToUpper(myTitle));     

}
//Result: To Proper Casing
//Result: to proper casing
//Result: TO PROPER CASING

2 Comments

  • No need for System.Threading, use CultureInfo.CurrentCulture to get the thread's culture.

  • Thanks for this post. I have been curious about title-casting for a while since I noticed times at work where I could use it as user eye-candy. I now have the answer. I do not look at System.Globalization a lot since my line of work does not require globalization; now I have a good reason to. Nice dandy post.

Comments have been disabled for this content.