Microsoft KB Archive/312897

= How to convert string to lowercase, uppercase, or title (proper) case by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 312897

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q312897





For a Microsoft Visual C# .NET version of this article, see 312890.

IN THIS TASK
SUMMARY Convert String Using the String Class or Visual Basic Functions
 * Convert a String to Uppercase
 * Convert a String to Lowercase
 * Convert a String to Title Case

Convert String Using the TextInfo Class Step-by-Step Example Note on the InvariantCulture Property REFERENCES



SUMMARY
This article demonstrates how to convert a string to uppercase, lowercase, or title (or proper) case.

Visual Basic .NET or Visual Basic 2005 offers several ways to convert strings. If you use the Microsoft .NET Framework classes, you can use methods of the String and TextInfo classes. Visual Basic .NET or Visual Basic 2005 also includes the built-in UCase, LCase, and StrConv functions that serve the same purpose.

Although you can use the String class to convert a string to uppercase or lowercase, you must use a method in the System.Globalization.TextInfo class to convert a string to title case.

back to the top

Convert String Using the String Class or Visual Basic Functions
This section describes how to use the methods of the String class and the Visual Basic built-in functions (UCase, LCase and StrConv) to convert a string to uppercase, lowercase, and title case.

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. You can also use the UCase or StrConv function. For example: Dim upper as String = &quot;converted from lowercase&quot; Console.WriteLine(upper.ToUpper) Console.WriteLine(UCase(upper)) Console.WriteLine(StrConv(upper,VbStrConv.UpperCase)) 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. You can also use the LCase or StrConv function. For example: Dim lower as String = &quot;CONVERTED FROM UPPERCASE&quot; Console.WriteLine(lower.ToLower) Console.WriteLine(LCase(lower)) Console.WriteLine(StrConv(lower,VbStrConv.LowerCase)) back to the top

Convert a String to Title Case
To convert a string to title (or proper) case, pass the string to the StrConv function with a constant that identifies the operation to be performed. For example: Dim title as String = &quot;converted to title case&quot; Console.WriteLine(StrConv(title, VbStrConv.ProperCase)) back to the top

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

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

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: Dim curCulture As CultureInfo = Thread.CurrentThread.CurrentCulture Dim tInfo As TextInfo = curCulture.TextInfo The following code sample illustrates how to call the three string conversion methods of the TextInfo class: Dim title as String = &quot;converted using textinfo&quot; Console.WriteLine(tInfo.ToTitleCase(title)) Console.WriteLine(tInfo.ToLower(title)) Console.WriteLine(tInfo.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 Basic .NET or in Visual Basic 2005.  Replace the code in Module1.vb with the following code: Imports System.Globalization Imports System.Threading Module Module1

Public Sub main

Dim title As String = &quot;this is my converted string&quot;

Console.WriteLine(&quot;Built-in Methods&quot;) Console.WriteLine(&quot;&quot;) Console.WriteLine(UCase(title)) Console.WriteLine(LCase(title)) Console.WriteLine(StrConv(title, VbStrConv.UpperCase)) Console.WriteLine(StrConv(title, VbStrConv.LowerCase)) Console.WriteLine(StrConv(title, VbStrConv.ProperCase)) Console.WriteLine

Console.WriteLine(&quot;String Class&quot;) Console.WriteLine(&quot;&quot;) Console.WriteLine(title.ToUpper) Console.WriteLine(title.ToLower) Console.WriteLine

Console.WriteLine(&quot;TextInfo Class&quot;) Console.WriteLine(&quot;--&quot;) 'Get culture information from current thread. Dim curCulture As CultureInfo = Thread.CurrentThread.CurrentCulture 'Create TextInfo object. Dim tInfo As TextInfo = curCulture.TextInfo 'Convert to uppercase. Console.WriteLine(tInfo.ToUpper(title)) 'Convert to lowercase. Console.WriteLine(tInfo.ToLower(title)) 'Convert to title case. Console.WriteLine(tInfo.ToTitleCase(title)) End Sub

End Module  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 can 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: Dim curCulture As CultureInfo = Thread.CurrentThread.CurrentCulture To use InvariantCulture in the same scenario, use the following code: Dim curCulture As CultureInfo = CultureInfo.InvariantCulture back to the top

