Mohanapriya R Mohanapriya R
Updated date Feb 15, 2024
In this article, we will learn how to convert text from Title Case to a regular string using C#. This article provides a simple yet effective program along with step-by-step explanations and sample outputs.

Understanding Title Case:

Title Case is a style of capitalization where the first letter of each word in a sentence is capitalized. For example, "The Quick Brown Fox Jumps Over The Lazy Dog" is in Title Case.

Converting Title Case to String in C#:

To convert text from Title Case to a regular string format in C#, we can utilize the TextInfo.ToTitleCase() method available in the System.Globalization namespace. This method converts the specified string to Title Case format. However, to convert it back to a standard string format, we need to manipulate the casing of each word individually.

Below is a sample C# program demonstrating how to convert Title Case to a standard string format:

using System;
using System.Globalization;

class Program
{
    static void Main(string[] args)
    {
        string titleCaseText = "The Quick Brown Fox Jumps Over The Lazy Dog";
        string[] words = titleCaseText.Split(' ');

        TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;

        string regularString = "";

        foreach (string word in words)
        {
            regularString += textInfo.ToLower(word[0]) + word.Substring(1) + " ";
        }

        regularString = regularString.Trim();

        Console.WriteLine("Converted String:");
        Console.WriteLine(regularString);
    }
}

Output:

Converted String:
the quick brown fox jumps over the lazy dog
  • We start by defining a sample string titleCaseText containing text in Title Case.
  • We split the string into individual words using the Split() method.
  • We create an instance of the TextInfo class to access text casing methods.
  • Then, we iterate through each word, converting the first letter to lowercase and appending the rest of the word as is.
  • Finally, we trim any trailing spaces and print the converted string.

Conclusion:

In this article, we have explored how to convert text from Title Case to a standard string format using C#. By leveraging the TextInfo.ToTitleCase() method and manipulating individual word casings, we can easily achieve this conversion. This knowledge can be applied in various scenarios where text processing or formatting is required.

Comments (0)

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