Microsoft KB Archive/815783

= How to convert string to lowercase, to uppercase, or to title case by using Visual C++ =

Article ID: 815783

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-







For a Microsoft Visual Basic .NET version of this article, see 312897.

IN THIS TASK

 * Summary
 * Requirements
 * Convert a String by Using the String Class
 * Convert a String to Uppercase
 * Convert a String to Lowercase
 * Convert a String by Using the TextInfo Class
 * Step-by-Step Example
 * The InvariantCulture Property
 * References



SUMMARY
This article describes how to convert a string to uppercase, to lowercase, or to title (proper) case. Microsoft Visual C++ .NET offers several ways to convert strings. If you use the Microsoft .NET Framework classes, you can use the methods of the String class and the TextInfo class. Although you can use the String class to convert a string to uppercase or to lowercase, you must use a method in the System.Globalization.TextInfo class to convert a string to title case.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Visual C++ .NET Programming Concepts
 * Visual C++ 2005 Programming Concepts

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Visual C++ .NET
 * Visual C++ 2005

back to the top

Convert a String by Using the String Class
This section describes how to use the methods of the String class to convert a string to uppercase and to lowercase.

back to the top

Convert a String to Uppercase
The String class has a static method that is named ToUpper. You can use this method to convert a string to uppercase as follows: String *lower = S&quot;this is a string in lowercase&quot;; Console::WriteLine(lower->ToUpper); Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
 * 1) Click Project, and then click   Properties.

Note  is a placeholder for the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

These steps apply to the whole article.

back to the top

Convert a String to Lowercase
The ToLower method is the complement of the ToUpper method. The ToLower method converts a string to lowercase as follows. String *upper = S&quot;THIS IS A STRING IN UPPERCASE&quot;; Console::WriteLine(upper->ToLower); back to the top

Convert a String by Using the TextInfo Class
This section describes how to use the TextInfo class to convert strings. Because you can use the conversion methods in the TextInfo class to control culture information, you may want to use this class when you must specify particular culture settings.

The TextInfo class is member of the System.Globalization namespace. TextInfo provides the ToUpper method, the ToLower method, and the ToTitleCase method for conversion to uppercase, to lowercase, and to title case, respectively. Unlike the methods of the String class, the TextInfo methods are not static methods and they require an instance of the class.

In most situations, you can use the culture that is currently in use. Culture information is a property of the thread where 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. After you do this, you can create the TextInfo object as follows: CultureInfo *cultInfo = Thread::CurrentThread->CurrentCulture; TextInfo *txtInfo = cultInfo->TextInfo; The following code sample illustrates how to call the three string conversion methods of the TextInfo class: String *lower = S&quot;this is a example to illustrate string conversion&quot;; CultureInfo *cultInfo = Thread::CurrentThread->CurrentCulture; TextInfo *txtInfo = cultInfo->TextInfo; Console::WriteLine(S&quot;String in Title case is {0}&quot;,txtInfo->ToTitleCase(lower)); If you must create or make changes to 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
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In Visual C++ .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Application under Templates.

In Visual C++ .NET 2003, click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates.

In Visual C++ 2005, click Visual C++ under Project Types, and then click CLR Console Application under Templates. In the Name text box, type StringConversion, and then click OK.  Open the StringConversion.cpp file, and then replace the existing code with the following code: using namespace System; using namespace System::Globalization; using namespace System::Threading; int _tmain {   String *lower = S&quot;this is a example to illustrate string conversion&quot;; String *upper; Console::WriteLine(S&quot;The initialized string is {0}&quot;, lower); upper = lower->ToUpper; Console::WriteLine(S&quot;String in upper case is {0}&quot;, upper); Console::WriteLine(S&quot;String converted to lower is {0}&quot;,upper->ToLower); CultureInfo *cultInfo = Thread::CurrentThread->CurrentCulture; TextInfo *txtInfo = cultInfo->TextInfo; Console::WriteLine(S&quot;String in Sentence case is {0}&quot;,txtInfo->ToTitleCase(lower)); return 0; }   Press the CTRL+F5 key combination to build and to run the project.</li></ol>
 * 1) include &quot;stdafx.h&quot;
 * 2) using <mscorlib.dll>

Notice the results in the Console window.

back to the top

The InvariantCulture Property
When you use the Globalization namespace to convert data, and you store the converted data instead of displaying it for the user, you can use the InvariantCulture property of the CultureInfo class.

The InvariantCulture property is neither a neutral nor a specific culture; it is a culture that is culture-insensitive. If you use the InvariantCulture property 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, see the &quot;References&quot; section of this article.

The following code sample uses the CultureInfo properties of the current thread: CultureInfo *cultInfo = Thread::CurrentThread->CurrentCulture; To use InvariantCulture in the same scenario, use the following code: CultureInfo *cultInfo = CultureInfo::InvariantCulture; back to the top

<div class="references_section">