Convert a String to Title Case
The String class does not include a method that converts a string to title case. The ToTitleCase method resides in the TextInfo class, which is a member of the System.Globalization namespace. Unlike the ToUpper and ToLower methods of the String class, the ToTitleCase method is not a static method and requires an instance of the class.
When you use the TextInfo class, you must specify cultural information. In most situations, you can default to the culture that is currently in use. Culture information is a property of the thread on which the code is running. To obtain the culture information, you must gain access to the current thread and retrieve the CurrentCulture property from that thread. Once you accomplish this, you can create the TextInfo object. For example:
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
The TextInfo class also includes the ToUpper and ToLower methods. Use these methods of TextInfo if you need to specify culture options.
Console.WriteLine(textInfo.ToTitleCase(title));
Console.WriteLine(textInfo.ToLower(title));
Console.WriteLine(textInfo.ToUpper(title));
If you need to create or manipulate strings that have specific culture settings, you can use one of the overloaded constructors of the TextInfo class to create strings with any of the available culture options.
Courtesy: http://support.microsoft.com/kb/312890
Thursday, March 12, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment