What is a Title Case?
The title case involves capitalizing the first letter of each word while keeping the rest of the letters in lowercase. For instance, "hello world" becomes "Hello World" in the title case. This not only enhances the visual appeal of the text but also adheres to conventional writing styles.
How to Convert Strings to Title Case using C#?
To implement title case transformation in C#, you can use the TextInfo.ToTitleCase
method from the CultureInfo
class. This method takes care of capitalizing the first letter of each word according to the specified cultural rules.
using System;
using System.Globalization;
class Program
{
static void Main()
{
string input = "hello world";
string titleCaseOutput = ToTitleCase(input);
Console.WriteLine("Original String: " + input);
Console.WriteLine("Title Case: " + titleCaseOutput);
}
static string ToTitleCase(string input)
{
TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
return textInfo.ToTitleCase(input);
}
}
Output:
Original String: hello world
Title Case: Hello World
In this program, the ToTitleCase
method is called with the input string, and the result is printed to the console.
Comments (0)