Surendra Sharma

Surendra Sharma

Search This Blog

Sunday, August 16, 2015

How to convert first letter of every word in a sentence to upper in C#

I come across with simple but very interesting problem where I have to make first letter of every word in a sentence to capital. For example if I have sentence "top 5 most advance computer" then it should be displayed as "Top 5 Most Advance Computer".

There are lots of ways to do this 

·         Write for loop and do the string operation
·         Call the VB.NET methods etc.

But I found LINQ help me to do this in simple and clean manner. Here is method for this

public static string ConvertFirstCharOfWordToUpper(string str)
    return string.Join(" ", str.Split(' ').Select(x => x.First().ToString().ToUpper() + x.ToLower().Substring(1)));
Please leave your comments or share this code if it’s useful for you.

1 comment:

  1. Why don't you use TextInfo.ToTitleCase ?