Capitalize First Letter of each Words of string in C#

Capitalize First Letter of each Words of string in C#

Hello folks, sometimes we may have requirement like to haveĀ Capitalize First Letter of each Words of string in C#, like we have full name of person or employee, which to be stored in database in proper manner. Ex dHruV SHeth must be stored in Database like Dhruv Sheth.

To achieve we could write our own Extension method and convert each of first latter in string. But we have ready made function in TextInfo which isĀ ToTitleCase.

 

System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(string val);

 

using System;

namespace NullableTypesCsharp
{
    class Program
    {
        static void Main(string[] args)
        {

            string strData = @"techno thirsty is a nIcE tEchNicle bLoG";

            strData = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(strData.ToLower());

            //Output: Techno Thirsty Is A Nice Technicle Blog
            Console.WriteLine(strData);

            Console.ReadKey();
        }
    }
}

 

Leave a Reply