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

Article translations Article translations
Article ID: 312890 - View products that this article applies to.
This article was previously published under Q312890
Expand all | Collapse all

On This Page

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.

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.

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());
				

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());
				

Convert String Using the TextInfo Class

This section describes how to use the TextInfo class to convert a string to title case.

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.

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.

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;
				

REFERENCES

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

Properties

Article ID: 312890 - Last Review: July 21, 2008 - Revision: 3.1
APPLIES TO
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
Keywords: 
kbsweptvs2008 kbhowtomaster kbsample KB312890

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com