.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Convert a string to Title Case in C#

Posted By:Suthish Nair       Posted Date: August 29, 2010    Points: 15    Category: C#    URL: http://www.dotnetspark.com  

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("dotnetspark"));

  Response.Write(textInfo.ToTitleCase("dot Net Spark"));

  Response.Write(textInfo.ToTitleCase("dot@nets$park").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: dotnetspark to DotNetSpark you can achieve this by using special characters.

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


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend