C# ToTitleCase MethodUse the ToTitleCase method from System.Globalization and understand the drawbacks of this method.
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.
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.
Required input, output:
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.
C# program that uses ToTitleCase
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.
Uppercase First Letter
Note This method was suggested by a reader as an alternative to the more elaborate implementations.
A summary. We looked at the ToTitleCase method on the TextInfo type. ToTitleCase can simplify your program, but it cannot be easily customized.
© 2007-2021 sam allen. see site info on the changelog