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.
using System;
using System.Globalization;
using System.Threading;
namespace csConsole
{
public class MyClass
{
public static void Main()
{
string title="this is my converted string";
Console.WriteLine("String Class");
Console.WriteLine("------------");
//Convert string to uppercase.
Console.WriteLine(title.ToUpper());
//Convert string to lowercase.
Console.WriteLine(title.ToLower());
Console.WriteLine();
Console.WriteLine("TextInfo Class");
Console.WriteLine("--------------");
//Get the culture property of the thread.
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
//Create TextInfo object.
TextInfo textInfo = cultureInfo.TextInfo;
//Convert to uppercase.
Console.WriteLine(textInfo.ToUpper(title));
//Convert to lowercase.
Console.WriteLine(textInfo.ToLower(title));
//Convert to title case.
Console.WriteLine(textInfo.ToTitleCase(title));
}
}
}
Note on the InvariantCulture Property
When you use the Globalization namespace to convert data, if you store the converted data instead of displaying it for the user, you may want to use the InvariantCulture property of CultureInfo class.
InvariantCulture is neither a neutral nor a specific culture; it is a culture that is culture-insensitive. If you use InvariantCulture when you store data, the data is stored in a consistent manner, regardless of any specific user or cultural system settings that may be in effect. For more information, refer to the References section.
The preceding code sample uses the CultureInfo properties of the current thread:
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
To use InvariantCulture in the same scenario, use the following code:
CultureInfo cultureInfo = CultureInfo.InvariantCulture;