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


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.

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.

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 = "converted from lowercase"

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 = "CONVERTED FROM UPPERCASE"

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 = "converted to title case"
Console.WriteLine(StrConv(title, VbStrConv.ProperCase))

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 = "converted using textinfo"
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 Basic .NET or in Visual Basic 2005.
  2. 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 = "this is my converted string"

    Console.WriteLine("Built-in Methods")
    Console.WriteLine(StrConv(title, VbStrConv.UpperCase))
    Console.WriteLine(StrConv(title, VbStrConv.LowerCase))
    Console.WriteLine(StrConv(title, VbStrConv.ProperCase))

    Console.WriteLine("String Class")

    Console.WriteLine("TextInfo Class")
    '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.
    'Convert to lowercase.
    'Convert to title case.
    End Sub

    End Module
  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 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


For additional information, refer to the following topics in the Microsoft Visual Studio .NET or Visual Studio 2005 Online Help documentation:

  • Using the InvariantCulture Property
  • TextInfo Class
  • CultureInfo Class
  • String Class

ID do Artigo: 312897 - Última Revisão: 6 de dez de 2006 - Revisão: 1