Home
C#
String ToTitleCase Method
This page was last reviewed on 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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 17, 2022 (simplify).
Home
Changes
© 2007-2024 Sam Allen.