Home
C#
String ToTitleCase Method
Updated Nov 17, 2022
Dot Net Perls
ToTitleCase. This C# method makes each word in a string title case—it capitalizes each word in a string. No custom code is needed to call ToTitleCase.
C# method notes. You could develop character-based algorithms to accomplish this goal. But the System.Globalization namespace provides ToTitleCase, which can be simpler.
TextInfo
Required output. Consider a simple string in C# like "purple fish." The string has 2 words separated with a space. Each word should have an uppercase letter.
purple fish -> Purple Fish
Example. This program shows how to call ToTitleCase. To access CultureInfo, you need to include the System.Globalization namespace. The input here is "purple fish."
And The output is "Purple Fish." The first letters of the 2 words are converted to uppercase.
using System; using System.Globalization; class Program { static void Main() { string value = "purple fish"; string titleCase = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value); Console.WriteLine(titleCase); } }
Purple Fish
A discussion. When should ToTitleCase 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. When performance is critical, caching with memoization could help.
String Uppercase First Letter
Memoization
A summary. We looked at the ToTitleCase method on the TextInfo type. ToTitleCase can simplify your program, but it cannot be easily customized.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 17, 2022 (simplify).
Home
Changes
© 2007-2025 Sam Allen