Blog

Convert a string to Title Case in C#

Posted by Suthish Nair Blogs | String in C# Sep 01, 2010
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.

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.

COMMENT USING
PREMIUM SPONSORS
Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
Employers - Post Free Jobs