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 topRequirements
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 topConvert 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"this is a string in lowercase";
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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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:
These steps apply to the whole article.
back to the topConvert 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"THIS IS A STRING IN UPPERCASE";
Console::WriteLine(upper->ToLower());
back to the topConvert 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"this is a example to illustrate string conversion";
CultureInfo *cultInfo = Thread::CurrentThread->CurrentCulture;
TextInfo *txtInfo = cultInfo->TextInfo;
Console::WriteLine(S"String in Title case is {0}",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
topStep-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:
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
using namespace System::Threading;
int _tmain()
{
String *lower = S"this is a example to illustrate string conversion";
String *upper;
Console::WriteLine(S"The initialized string is {0}", lower);
upper = lower->ToUpper();
Console::WriteLine(S"String in upper case is {0}", upper);
Console::WriteLine(S"String converted to lower is {0}",upper->ToLower());
CultureInfo *cultInfo = Thread::CurrentThread->CurrentCulture;
TextInfo *txtInfo = cultInfo->TextInfo;
Console::WriteLine(S"String in Sentence case is {0}",txtInfo->ToTitleCase(lower));
return 0;
}
- Press the CTRL+F5 key combination
to build and to run the project.
Notice the results in the Console window.
back to the topThe 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
"References" 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