.Net Tips – String.ToTitleCase() extension methods

As you most likely know already, there are ToLower() and ToUpper() methods on the String class in C#, but a method to convert a string to ‘Title Case’ is sadly missing.

Support for this useful conversion exists in the TextInfo class which has a ToTitleCase method, but you can’t instantiate a new instance of the TextInfo type as there is no public constructor. Instead you will need to first create a new CultureInfo object:

var str = "wAr AnD peaCE";
var textInfo = new CultureInfo("en-US").TextInfo;
var titleCaseStr = textInfo.ToTitleCase(str);
Console.WriteLine(titleCaseStr);
/* this outputs
War And Peace
*/

Seeing as the CultureInfo class doesn’t have a parameter-less constructor, it means if you wish to do the above you have to provide a ‘default’ culture. Instead of hard-coding a default culture in your code, I’d recommend using the current culture of the executing thread instead:

var cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;

That way, if you don’t care about the culture info you’re using, you will just default to whatever culture info your current thread is using, much cleaner than the alternative!

Better still, if you’re gonna be needing title case conversion often, why not just add some extension methods to the Sting type:

public static class StringExtension
{
    /// <summary>
    /// Use the current thread's culture info for conversion
    /// </summary>
    public static string ToTitleCase(this string str)
    {
        var cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
        return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
    }

    /// <summary>
    /// Overload which uses the culture info with the specified name
    /// </summary>
    public static string ToTitleCase(this string str, string cultureInfoName)
    {
        var cultureInfo = new CultureInfo(cultureInfoName);
        return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
    }

    /// <summary>
    /// Overload which uses the specified culture info
    /// </summary>
    public static string ToTitleCase(this string str, CultureInfo cultureInfo)
    {
        return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
    }
}

You may have noticed from the remarks in the MSDN page for the TextInfo.ToTitleCase method that the method doesn’t provide proper casing to convert a word that is entirely uppercase, such as an acronym.

It’s for this reason that I have added the ToLower() call in each of the above extension methods.

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter


Hi, I’m Yan. I’m an AWS Serverless Hero and the author of Production-Ready Serverless.

I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.


Check out my new course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, callbacks, nested workflows, design patterns and best practices.

Get Your Copy