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("aspnet"));
           Response.Write(textInfo.ToTitleCase("asp Net"));
           Response.Write(textInfo.ToTitleCase("asp@net$asp").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: Aspnet to AspNet you can achieve this by using special characters. You can find the difference in above all outputs. Please post your comments.

Published Saturday, August 28, 2010 9:51 PM by Suthish Nair

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)