Convert a string to Title Case in C#

There is no direct method like ToUpper(), ToLower() for Title Case. But using CultureInfo and  TextInfo classes we can do Title case of a string. Below method will convert the first character of each word to uppercase.

The references used:

using System.Globalization;

using System.Threading;

 

protected void Page_Load(object sender, EventArgs e)

{

   CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;

   TextInfo textInfo = cultureInfo.TextInfo;

   Response.Write(textInfo.ToTitleCase("csharpCornerHome<br />"));

  Response.Write(textInfo.ToTitleCase("csharp Corner Home"));

  Response.Write(textInfo.ToTitleCase("csharp@corner$home<br />").Replace("@",

"").Replace("$", ""));

}

 

Note:

The method will convert the first letter to uppercase and rest all letters to lowercase.

If you have a single word and want to convert some of the characters to uppercase, for example: Csharpcornerhome to CsharpCornerHome you can achieve this by using special characters.

You can find the difference in above all outputs. Please post your comments.

Ebook Download
View all
Learn
View all