Updated on

To convert a string to title case in C#, we call ToTitleCase() on a TextInfo object: new CultureInfo("en-US", false).TextInfo.ToTitleCase("a tale of two cities") returns “A Tale Of Two Cities”.

In title case, the first character of each word is uppercase and the rest of the word is lowercase. Some people call it proper case. The method has a few quirks, and we’ll go through each one with an example.

To download the source code for this article, you can visit our GitHub repository.

Let’s dive in.

How Does TextInfo.ToTitleCase Work?

TextInfo.ToTitleCase() takes a string and returns a copy in which each word starts with an uppercase letter and the rest of the word is lowercase. It lives in the System.Globalization namespace, and we call it on a TextInfo object that we get from a CultureInfo.

The string type has no title case method of its own, so this is the built-in way to do it in .NET.

The culture matters because it carries the casing rules. With the “en-US” culture, “a tale oF tWo citIes” becomes “A Tale Of Two Cities”, and the mixed-case letters inside each word are fixed too.

Three behaviours catch people out. A word written entirely in uppercase stays that way, because the method treats it as an acronym. Short words such as “of” and “the” get a capital like any other word. And the same input can come back different under another culture, with Turkish as the usual example.

We cover each of them below.

.NET has an inbuilt TextInfo.ToTitleCase() method that we can use to convert strings to title case:

public string ToTitleCase (string str);

This method exists within System.Globalization namespace. However, TextInfo.ToTitleCase() cannot convert words that are entirely in uppercase to title case. We will see how to do that later on.

To use the ToTitleCase() method, we first need to create a TextInfo:

using System.Globalization;

var textinfo = new CultureInfo("en-US", false).TextInfo;

As you can see, we create a TextInfo based on the “en-US” culture. We can also use the culture of our current location when declaring the TextInfo instance:

var textinfo = CultureInfo.CurrentCulture.TextInfo;

That makes the result depend on the machine the code runs on, which we look at in the culture section below.

Afterward, we can call ToTitleCase() and convert our desired string to a title case:

Console.WriteLine(textinfo.ToTitleCase("a tale oF tWo citIes"));

Console.WriteLine(textinfo.ToTitleCase("harry potter and the Deathly hallows"));

With the “en-US” culture, this prints:

A Tale Of Two Cities
Harry Potter And The Deathly Hallows

If we only need the first letter of a string in uppercase, we have a separate article on how to capitalize only the first letter of a string.

Converting an Entire Uppercase String to Title Case

As we said earlier, ToTitleCase() cannot convert words that are entirely uppercase such as acronyms. Microsoft Learn describes the method as converting a string to title case “except for words that are entirely in uppercase, which are considered to be acronyms”. So textinfo.ToTitleCase("OLIVER TWIST") returns “OLIVER TWIST” untouched.

To convert such words, we lowercase the string first, with the same TextInfo:

Console.WriteLine(textinfo.ToTitleCase(textinfo.ToLower("OLIVER TWIST")));

This prints “Oliver Twist”.

We use textinfo.ToLower() here instead of string.ToLower() on purpose. string.ToLower() reads the culture of the machine, so on a computer set to Turkish it turns “OLIVER TWIST” into “olıver twıst”, with a dotless ı, and we end up with “Olıver Twıst”. Calling ToLower() on the same TextInfo keeps both steps under one culture.

Lowercasing first has a price, though. It also flattens the real acronyms:

Console.WriteLine(textinfo.ToTitleCase(textinfo.ToLower("THE NASA MISSION")));

This prints “The Nasa Mission”. If the text mixes acronyms with normal words, like “the NASA mission”, it’s better to leave it as it is: ToTitleCase() returns “The NASA Mission” without any help.

Furthermore, it is worth mentioning that ToTitleCase method is not linguistically correct. Compared to our previous examples, the linguistically correct outcome would be:

A Tale of Two Cities

Harry Potter and the Deathly Hallows

Nonetheless, ToTitleCase() is a simpler way of converting strings to title case, and in the next section we add the missing rule ourselves.

How Do We Keep Small Words Like “of” and “the” Lowercase?

ToTitleCase() has no option for this, so we write a small extension method on top of it, in a new TitleCaseExtensions.cs file:

using System.Globalization;

public static class TitleCaseExtensions
{
    private static readonly HashSet<string> MinorWords = new(StringComparer.OrdinalIgnoreCase)
    {
        "a", "an", "and", "as", "at", "but", "by", "for", "in", "nor", "of", "on", "or", "the", "to"
    };

    public static string ToTitleCaseWithMinorWords(this string input, TextInfo textInfo)
    {
        var words = textInfo.ToTitleCase(textInfo.ToLower(input)).Split(' ');

        for (var i = 1; i < words.Length - 1; i++)
        {
            if (MinorWords.Contains(words[i]))
            {
                words[i] = textInfo.ToLower(words[i]);
            }
        }

        return string.Join(' ', words);
    }
}

First, we lowercase the input and run it through ToTitleCase(), so every word starts with a capital. Then we split the result on spaces and walk the words between the first and the last one. When a word is in our MinorWords set, we lowercase it again. The first and the last word keep their capital, which is the usual title rule. Finally, we join the words back together.

Our HashSet<T> uses StringComparer.OrdinalIgnoreCase, so “Of” and “of” both match.

Let’s run it on the two book titles from earlier:

Console.WriteLine("a tale oF tWo citIes".ToTitleCaseWithMinorWords(textinfo));
Console.WriteLine("harry potter and the Deathly hallows".ToTitleCaseWithMinorWords(textinfo));

With the “en-US” culture, we get the linguistically correct version from the previous section:

A Tale of Two Cities
Harry Potter and the Deathly Hallows

The list of small words is our choice, and style guides don’t agree on it. If a project follows a specific guide, we change the set and nothing else.

The method still lowercases everything first, so it shares the acronym problem from the previous section. And it splits on single spaces only, which is enough for titles. For text with tabs or line breaks, there are other ways to split a string.

Why Does the Culture Change the Result?

ToTitleCase() follows the casing rules of the TextInfo we call it on, and the cultures don’t all agree on what an uppercase or lowercase letter is. Turkish is the classic case. It has a dotted and a dotless i, so the uppercase of “i” is “İ” and the lowercase of “I” is “ı”.

Let’s run “a tale oF tWo citIes” under Turkish, and then under the invariant culture. The first line lets a Windows console print the Turkish letters:

Console.OutputEncoding = System.Text.Encoding.UTF8;
var turkish = new CultureInfo("tr-TR", false).TextInfo;
Console.WriteLine(turkish.ToTitleCase("a tale oF tWo citIes"));
Console.WriteLine(turkish.ToTitleCase("istanbul"));
Console.WriteLine(CultureInfo.InvariantCulture.TextInfo.ToTitleCase("a tale oF tWo citIes"));

The output:

A Tale Of Two Citıes
İstanbul
A Tale Of Two Cities

The capital “I” inside “citIes” becomes a dotless “ı”, because that is the lowercase of “I” in Turkish. “istanbul” gets a dotted capital “İ”, which is exactly what a Turkish reader expects. Both results follow Turkish casing rules: right for Turkish text, wrong for an English title like the first one.

So which culture should we pick? For text that people read in their own language, we use their culture. For text that only code reads, such as keys, file names or generated identifiers, CultureInfo.InvariantCulture gives the same result on every machine. What we should avoid is CultureInfo.CurrentCulture by accident, because then the output depends on whichever computer runs the code. That includes the unit tests: a test that checks CurrentCulture output can pass on our machine and fail on a colleague’s.

The false we pass to the CultureInfo constructor matters too. It tells .NET to ignore the changes a user made to that culture in the Windows regional settings, such as a different date format, and use the culture’s default data instead.

Which Inputs Does ToTitleCase Get Wrong?

The method looks at letters one word at a time and knows nothing about what the words mean. A few inputs show it:

Console.WriteLine(textinfo.ToTitleCase("the 2nd edition"));
Console.WriteLine(textinfo.ToTitleCase("my iPhone"));
Console.WriteLine(textinfo.ToTitleCase("mcdonald's menu"));
Console.WriteLine(textinfo.ToTitleCase("x-ray vision"));

With the “en-US” culture, we get:

The 2Nd Edition
My Iphone
Mcdonald's Menu
X-Ray Vision

“2nd” becomes “2Nd”, because the method capitalizes the first letter it finds in a word, even when a digit comes before it. “iPhone” loses its inner capital, since every letter after the first gets lowercased. “Mcdonald’s” has no way of knowing about the capital D. The hyphen, on the other hand, works the way we’d want, and so does the apostrophe in “mcdonald’s”, which doesn’t turn the “s” into a capital.

None of this can be fixed with a setting. For names and brands, the practical answer is a short list of exceptions we replace after the call, or leaving those titles to a human.

Conclusion

In this article, we have learned how we can convert strings to title case in C# using the TextInfo.ToTitleCase() method. We’ve also learned how to convert all-uppercase strings to title case strings, and why lowercasing them with the same TextInfo gives the same result on any machine.

When the small words have to stay lowercase, a short extension method on top of ToTitleCase() does the job. And when the result matters to a reader, we pick the culture on purpose instead of leaving it to whatever machine runs the code.

If the next step is code identifiers, see how to convert a title case string to camelCase.

Tested with .NET 10.0.10 and MSTest 4.5.2.