"We live in an exciting time. The Internet has become almost ubiquitous throughout much of the world, bringing with it freedom of information and an unprecedented power to all. My passion is seeing businesses leverage that power effectively and economically." - Dave Ranck

May
05

Title Case in C#

By Dave Ranck

Here a quick snippet to convert text to Title Case in C# that uses the TextInfo class. Note that strings with all caps will not be converted, so convert to lower case first.

// Defines the string with mixed casing.
string myString = "this iS a StrIng with MIXED CaSe";

// Create a TextInfo based on the "en-US" culture.
TextInfo tInfo = new CultureInfo("en-US", false).TextInfo;

// Changes a string to titlecase.
// Note the ToLower(), needed to convert words in all caps like MIXED
Console.WriteLine("\"{0}\" to titlecase: {1}", myString, tInfo.ToTitleCase(myString.ToLower()));

//Result: This Is A String With Mixed Case
Console.ReadLine();

// Defines the string with mixed casing.

string myString = “this iS a StrIng with MIXED CaSe”;

// Create a TextInfo based on the “en-US” culture.

TextInfo tInfo = new CultureInfo(“en-US”, false).TextInfo;

// Changes a string to titlecase.

// Note the ToLower(), needed to convert words in all caps like MIXED

Console.WriteLine(“\”{0}\” to titlecase: {1}”, myString, tInfo.ToTitleCase(myString.ToLower()));

//Result: This Is A String With Mixed Case

Console.ReadLine();

  • Share/Bookmark

Related posts:

  1. Find a String Between 2 Strings

Leave a Reply

Sponsored By :

 

 

Material in this site unless otherwise noted is Copyright David Ranck 2009, 2010