SUMMARY
This article demonstrates how to convert a string to uppercase, lowercase, or title (or proper) case.
Although you can use methods of the
String class to convert a string to uppercase or lowercase, you must use a method in the
TextInfo class of the
System.Globalization namespace to convert a string to title case. The
String class does not include a built-in method to convert a string to title case.
back to the top
Convert String Using the String Class
This section describes how to use the methods of the
String class to convert a string to uppercase and lowercase.
back to the top
Convert a String to Uppercase
The
String class has a static method named
ToUpper. You can use this method to convert a string to uppercase. For example:
string lower = "converted from lowercase";
Console.WriteLine(lower.ToUpper());
back to the top
Convert a String to Lowercase
The
ToLower method is the complement of the
ToUpper method.
ToLower converts a string to lowercase. For example:
string upper = "CONVERTED FROM UPPERCASE";
Console.WriteLine(upper.ToLower());
back to the top
Convert String Using the TextInfo Class
This section describes how to use the
TextInfo class to convert a string to title case.
back to the top
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.
back to the top
Step-by-Step Example
- Open a new Console Application project in Visual C# .NET or Microsoft Visual C# 2005.
- Replace the code in Class1.cs with the following code:
Note In Visual Studio 2005, the default file is Program.cs.
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));
}
}
}
- Press the CTRL+F5 key combination to build and run the project.
- Observe the results in the Console window.
back to the top
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;
back to the top