DCSIMG
C#, Converting string into "Title Case" - Pini Dayan

Pini Dayan

The best thing about a boolean is even if you are wrong, you are only off by a bit.

C#, Converting string into "Title Case"

Here is a nice a simple way I found to make a string "Title Case". What this means is that every word in a string will have it's first letter upper cased.

So, Here are the steps to accomplish this simple task:

1. Add using System.Globalization; to your code.

2. Create a TextInfo object. It has a ToTitleCase function we shall soon use.

TextInfo info = CultureInfo.CurrentCulture.TextInfo;

3. Convert the string you need to "Title Case".

Here is a simple sample:

static void Main(string[] args)
{
    TextInfo oInfo = CultureInfo.CurrentCulture.TextInfo;
    string sample = "the Is a nice way Of converting a STRing intO a title Case";
    Console.WriteLine(oInfo.ToTitleCase(sample));
}

And the result will be: The Is A Nice Way Of Converting A String Into A Title Case.

Enjoy.

Posted: May 13 2009, 08:44 AM by Pini Dayan | with 1 comment(s) |
תגים:,

Comments

יוסי said:

אדיר!!!

תודה

# May 24, 2009 2:49 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: