Home > ASP.NET > Proper case in vb.net

Proper case in vb.net

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 inTextInfo 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 ToUpperToLower, and ToTitleCasemethods for conversion to uppercase, lowercase, and title case respectively. Unlike the methods of the String class, theTextInfo 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 theCurrentCulture 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"
Example :

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

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: