HomeSearch

C# ToTitleCase Method

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

ToTitleCase

makes each word title case. It capitalizes each word in a string. You could develop character-based algorithms to accomplish this goal. But the System.Globalization namespace provides ToTitleCase, which can be simpler.TextInfo

Example.

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".

And: The output is "Dot Net Perls". The d, n, and p are converted to uppercase.

C# program that uses ToTitleCase 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

Discussion.

When should ToTitleCase be used? And when should a custom implementation be used? Programs often have edge cases. A custom implementation can provide better support for certain words. And 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.

We looked at the ToTitleCase method on the TextInfo type. There are other methods available on TextInfo. They are detailed in a separate article. ToTitleCase can simplify your program, but it cannot be easily customized.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.