Saturday, November 3, 2012

Converting a given string to Title Case

To convert string in title case is bit tedious and may require lot of code as there is no direct method available in C#.Net  in String class. So, here I am sharing a method, which will be the extension method for String class and can be used to convert given string to title case.
Here I am using CultureInfo from Globalization and ToTitleCase method of TextInfo class.


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

Hope reusing this will save lot of your time :)

No comments:

Post a Comment