Mohanapriya R Mohanapriya R
Updated date Aug 28, 2024
In this article, we will learn how to convert strings into title case in C#.
  • 1.9k
  • 0
  • 0

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. 

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

 

Comments (0)

There are no comments. Be the first to comment!!!