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.

post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.