You are currently offline, waiting for your internet to reconnect

How to convert strings to lower, upper, or title (proper) case by using Visual C#

This article was previously published under Q312890
For a Visual Basic .NET version of this article, see 312897.

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Globalization


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

  1. Open a new Console Application project in Visual C#.
  2. Replace the code in Program.cs with the following code:

    Note In Visual Studio .NET 2003, the default file is Class1.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));	}    }}					
  3. Press the CTRL+F5 key combination to build and run the project.
  4. 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
For additional information, see to the following topics in the Microsoft Visual Studio Online Help documentation:
  • Using the InvariantCulture Property
  • TextInfo Class
  • CultureInfo Class
  • String Class
back to the top
upper case lower

Article ID: 312890 - Last Review: 07/21/2008 19:40:07 - Revision: 3.1

Microsoft Visual C# 2008 Express Edition, Microsoft Visual C# .NET 2002 Standard Edition, Microsoft Visual C# 2005 Express Edition

  • kbsweptvs2008 kbhowtomaster kbsample KB312890