Capitalization of string

To convert string to title case (capitalize initial letters of the string), there is not string method to call. However, we can use the CultureInfo from Globalization and use the TextInfo’s ToTitleCase method.
Here is how it can be done in C#:

Add class StringExtensions to your root namespace and copy the code,All string variables in your project will now shows ToTitleCase method (extension method)


public static class StringExtensions
{
    public static string ToTitleCase(this string input)
    {
        System.Globalization.CultureInfo cultureInfo =
        System.Threading.Thread.CurrentThread.CurrentCulture;
        System.Globalization.TextInfo textInfo = cultureInfo.TextInfo;
        return textInfo.ToTitleCase(input.ToLower());
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s