Tuesday, October 14, 2008

Capitializing first letter of every word

The ToTitleCase method in .NET is not a part of String class but of TextInfo class that resides in System.Globalization namespace. This method converts the first character of every word into upper case. Here's how to use it,

String sHelpText = "aN eXamPle tEXT";
System.Globalization.CultureInfo oCInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Globalization.TextInfo oTextInfo = oCInfo.TextInfo;

return(oTextInfo.ToTitleCase(sHelpText.ToLower()));

Note: The .ToLower() Method is used to explicitly convert the string into lower case. According to MSDN - the title casing converts the first character into upper case, except for a word that is upper case in entirety. Read more on this here.


0 comments: