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 theCultureInfo
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)