.Net Tips – String.ToTitleCase() extension methods

Yan Cui

I help clients go faster for less using serverless technologies.

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.


Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game.
  2. Consulting: If you want to improve feature velocity, reduce costs, and make your systems more scalable, secure, and resilient, then let’s work together and make it happen.
  3. Join my FREE Community on Skool, where you can ask for help, share your success stories and hang out with me and other like-minded people without all the negativity from social media.

 

Leave a Comment

Your email address will not be published. Required fields are marked *