C# ToTitleCase

Lowercase and uppercase words

ToTitleCase makes each word title case. It capitalizes each word in a string in your C# program. Although you could develop character-based algorithms to accomplish this goal, the System.Globalization namespace provides the ToTitleCase method, which can be simpler.

Example

Note

This program shows how to call ToTitleCase and the string value it returns. To access CultureInfo, you need to include the System.Globalization namespace. The input to the program is "dot net perls"; the output is "Dot Net Perls". The d, n, and p are converted to uppercase.

This C# example program uses the ToTitleCase method from System.Globalization.

Program that uses ToTitleCase [C#]

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
	string value = "dot net perls";
	string titleCase = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
	Console.WriteLine(titleCase);
    }
}

Output

Dot Net Perls

Algorithms

Question and answer

When should this ToTitleCase method be used, and when should a custom implementation be used? Programs often have edge cases. In such situations, a custom implementation can provide better support for certain words. Additionally, performance can be improved in custom algorithms.

Uppercase First Letter

Note: This method was suggested by Atoki as an alternative to the more elaborate implementations.

Summary

The C# programming language

We looked at the ToTitleCase method on the TextInfo type in the C# language. There are other methods available on TextInfo; they are detailed on a separate article on this site. ToTitleCase can simply your program, but it is harder to customize to your needs than a custom algorithm.

TextInfo Method Tips String Type
.NET