Mohanapriya R Mohanapriya R
Updated date Aug 27, 2024
In this article, we will learn how to convert strings to proper case in C#.
  • 1.6k
  • 0
  • 0

 Converting Strings to Proper Case in C#

Proper case conversion involves capitalizing the first letter of each word in a given string. It ensures a consistent and polished appearance, often used in titles, names, or user interfaces for enhanced readability.

Simple C# program that takes an input string and converts it to a proper case:

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();

        string properCase = ConvertToProperCase(input);

        Console.WriteLine("Proper Case Output: " + properCase);
    }

    static string ConvertToProperCase(string input)
    {
        TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
        return textInfo.ToTitleCase(input.ToLower());
    }
}
  • We use the TextInfo class from the CultureInfo namespace, which provides information about the writing system.
  • The ToTitleCase method is then employed to convert the given string to proper case.
  • The input string is converted to lowercase first to ensure consistency.

Output:

 

Enter a string: hello world of programming
Proper Case Output: Hello World Of Programming

Comments (0)

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